Displaying an ASCII spiral in 6502

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
lightbeing
Posts: 11
Joined: 15 Oct 2023
Location: Paris (France)

Displaying an ASCII spiral in 6502

Post by lightbeing »

A challenge was recently organised on facebook.

The aim was to create a short BASIC program displaying an ASCII code progressively on the screen, following the shape of a spiral which starts at the centre of the screen. The challenge was opened to all computers.

I wrote a code in assembly for the Commodore 64. Whilst writing it, I figured out that it could be used actually on any computer based on the 6502 processor. It just needed some ajustments for the screen memory adresses and # of rows. The final shape of the spiral on the screen is a perfect square (25 x 25 on the Commodore 64, 25 being the # of rows).

It takes around 20,000 cycles. It's based on 4 subroutines (each computing the positions on the left, right, up and bottom) with ADC and SBC running in a loop, and self-modifying code. Nothing exceptional nor fantastic.

While writing it, I thought that a similar challenge could be organised just for the fun of sharing knowlege and practices. Anyone interested?
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Displaying an ASCII spiral in 6502

Post by BigEd »

Could you show us a sketch of the target shape? (Either as ascii art in CODE tags, or as an attached image)
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Displaying an ASCII spiral in 6502

Post by drogon »

lightbeing wrote:
A challenge was recently organised on facebook.

The aim was to create a short BASIC program displaying an ASCII code progressively on the screen, following the shape of a spiral which starts at the centre of the screen. The challenge was opened to all computers.

I wrote a code in assembly for the Commodore 64. Whilst writing it, I figured out that it could be used actually on any computer based on the 6502 processor. It just needed some ajustments for the screen memory adresses and # of rows. The final shape of the spiral on the screen is a perfect square (25 x 25 on the Commodore 64, 25 being the # of rows).

It takes around 20,000 cycles. It's based on 4 subroutines (each computing the positions on the left, right, up and bottom) with ADC and SBC running in a loop, and self-modifying code. Nothing exceptional nor fantastic.

While writing it, I thought that a similar challenge could be organised just for the fun of sharing knowlege and practices. Anyone interested?
Which group? I can't find it in my usual BASIC groups on FB...

But how can a spiral turn into a square?

Or do you mean like a turtle graphics spiral, turning 90° and increasing the side length by 1 every turn?

Like this, but with ASCII "graphics":
Screenshot_2024-01-11_18-14-40.png
Screenshot_2024-01-11_18-14-40.png (10.3 KiB) Viewed 6991 times
-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
User avatar
lightbeing
Posts: 11
Joined: 15 Oct 2023
Location: Paris (France)

Re: Displaying an ASCII spiral in 6502

Post by lightbeing »

The FINAL shape of the spiral is a square when the drawing has finished.

But each ASCII output during the drawing on the screen is performed as a rotation (left or right)

Here's how the final shape looks like with 205/206 PETSCII codes.

The facebook group is BASIC language programming:
https://www.facebook.com/groups/2057165187928233

The result of the challenge is here
https://magentacloud.de/s/nEcAdCPDiteLgPp
Attachments
vice-screen-2024011119332343.png
vice-screen-2024011119332343.png (5.82 KiB) Viewed 6983 times
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Displaying an ASCII spiral in 6502

Post by BigEd »

OK, so it's a spiral fill pattern, printing whatever (but the usual diagonal "maze" happens to look good)

By spiral fill I mean something like

Code: Select all

16 15 14 13
 5  4  3 12
 6  1  2 11
 7  8  9 10
User avatar
lightbeing
Posts: 11
Joined: 15 Oct 2023
Location: Paris (France)

Re: Displaying an ASCII spiral in 6502

Post by lightbeing »

BigEd wrote:
OK, so it's a spiral fill pattern, printing whatever (but the usual diagonal "maze" happens to look good)

By spiral fill I mean something like

Code: Select all

16 15 14 13
 5  4  3 12
 6  1  2 11
 7  8  9 10
Exactly.
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Displaying an ASCII spiral in 6502

Post by drogon »

I eventually found it on Facebook - dates back to November 2023.

https://www.facebook.com/10000937048512 ... 862283827/

This is Richard Russels entry in BBC Basic:

https://wasm.bbcbasic.net/bbcsdl.html?a ... s%2Fcompo4

Code: Select all

10 MODE 1
20 VDU 5,19,1,4,0,0,0
30 GCOL 0,129 : CLG
40 VDU 23,205,&80,&40,&20,&10,&08,&04,&02,&01
50 VDU 23,206,&01,&02,&04,&08,&10,&20,&40,&80
60 X = 608 : Y = 512 : N = 1
70 REPEAT
80 FOR I = 1 TO N : MOVE X,Y : VDU 205.5 + RND(1) : X = X+32 : T = INKEY(1) : NEXT
90 FOR I = 1 TO N : MOVE X,Y : VDU 205.5 + RND(1) : Y = Y+32 : T = INKEY(1) : NEXT
100 N = N+1
110 FOR I = 1 TO N : MOVE X,Y : VDU 205.5 + RND(1) : X = X-32 : T = INKEY(1) : NEXT
120 FOR I = 1 TO N : MOVE X,Y : VDU 205.5 + RND(1) : Y = Y-32 : T = INKEY(1) : NEXT
130 N = N+1
140 UNTIL N > 40
150 END

And, sorry to be a grump, but after taking far too long to try to find the original posting on Facebook and then having to wade through what I consider to be an utterly dreadful website with the results I have to say that frankly I just can't be bothered at this point.

I strongly suggest that in-future you provide direct links or just re-post the entire thing here so that those who care can see what you mean with the minimal of effort because life's just too short.

-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Displaying an ASCII spiral in 6502

Post by BigEd »

I think there’s an interesting challenge in optimising some 6502 code which makes coordinates for this kind of spiral. I’ve previously seen this spiral as an Ulam Spiral, where each point is used to indicate if a number is prime or not.
User avatar
lightbeing
Posts: 11
Joined: 15 Oct 2023
Location: Paris (France)

Re: Displaying an ASCII spiral in 6502

Post by lightbeing »

BigEd wrote:
I think there’s an interesting challenge in optimising some 6502 code which makes coordinates for this kind of spiral. I’ve previously seen this spiral as an Ulam Spiral, where each point is used to indicate if a number is prime or not.
That's what I tought. Optimizing the code could be fun.

On a computer with 40 colums, the rotation will move on the screen by adding or subtracting those values +1,+40, -1,-1,-40,-40,+1,+1,+1,+40,+40,+40 and so on.

The "fastest" way would be to builld a table with precalculated positions (high byte and low byte) but the table would take 1250 bytes (based on 25 rows). Furthermore, I tend to consider that precalculated positions is not really... "sexy".
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Displaying an ASCII spiral in 6502

Post by BigEd »

A table doesn't seem very interesting, that's for sure! Often these challenges have a size limitation, or will score on the basis of size, so that can push solutions towards ingenuity. The smallest code to do the job might be a good challenge, in fact... I don't think it's likely to be slow, in any case.
gfoot
Posts: 871
Joined: 09 Jul 2021

Re: Displaying an ASCII spiral in 6502

Post by gfoot »

I think we just need to move left one then down one, then right two and up two, then left three and down three, and so on. One way could be to store a direction vector starting with (-1,0) and rotate it left 90 degrees at each corner by swapping its elements and negating the new first element - so (-1,0) becomes (0,-1) then (1,0) then (0,1) and then back to (-1,0). The numbers are small enough that they don't wrap, which helps! Or it may make more sense to unroll the loop. I might sketch something a bit later if I get time.
plasmo
Posts: 1273
Joined: 21 Dec 2018
Location: Albuquerque NM USA

Re: Displaying an ASCII spiral in 6502

Post by plasmo »

lightbeing wrote:
The aim was to create a short BASIC program displaying an ASCII code progressively on the screen, following the shape of a spiral which starts at the centre of the screen. The challenge was opened to all computers.
Your topic has inspired me to this attempt at displaying ASCII spiral with a 6502 VGA controller. The screen is 640x480 monochrome. It is a 25MHz 6502 and screen is 32 rows x 80 columns characters in 8x15 fonts. 6502 spent 90% of its time painting the screen, so the spiral displaying task was done during the vertical retrace time--roughly equivalent to 2MHz 6502. The program is in 6502 assembly.
Bill
User avatar
lightbeing
Posts: 11
Joined: 15 Oct 2023
Location: Paris (France)

Re: Displaying an ASCII spiral in 6502

Post by lightbeing »

gfoot wrote:
I think we just need to move left one then down one, then right two and up two, then left three and down three, and so on. One way could be to store a direction vector starting with (-1,0) and rotate it left 90 degrees at each corner by swapping its elements and negating the new first element - so (-1,0) becomes (0,-1) then (1,0) then (0,1) and then back to (-1,0). The numbers are small enough that they don't wrap, which helps! Or it may make more sense to unroll the loop. I might sketch something a bit later if I get time.
Very interesting. Thank you. I get the idea of a direction vector. I think that's what I call a "quadrant indicator". The screen is divided in 4 quarters (upper right, down right, down left, upper right) in order to determine the actual position of the rotation and the branching of the code.
User avatar
lightbeing
Posts: 11
Joined: 15 Oct 2023
Location: Paris (France)

Re: Displaying an ASCII spiral in 6502

Post by lightbeing »

BigEd wrote:
A table doesn't seem very interesting, that's for sure! Often these challenges have a size limitation, or will score on the basis of size, so that can push solutions towards ingenuity. The smallest code to do the job might be a good challenge, in fact... I don't think it's likely to be slow, in any case.
Indeed. It's very fast. For my own code, I had to add a raster loop as a waiting loop in order to make the progress of the spiral visible to the naked eye.

You're right. The length of the code should be limited to a maximum number of bytes. Mine is 143 bytes long. Although it uses 7 bytes for the waiting loop, I have the feeling that it's not completely optimised and ingenious. I guess 200 bytes long is far enough.

I would also add the following rules in order to make the competition fair:
- pure 6502 legal codes
- use of screen memory and one ASCII/screen code
- use of "internal" routines prohibited
- direction of rotation may be right or left
- final shape displayed on screen is 25x25 (you can do more but it takes more cycles...)

If anyone is interested in participating or helping me in organising the challenge, just let me know.
Attachments
vice-video-2024011313094162.gif
vice-video-2024011313094162.gif (306.83 KiB) Viewed 6842 times
Post Reply