Skip to content

Commit

Permalink
use firmware calls for RND
Browse files Browse the repository at this point in the history
  • Loading branch information
ssg committed May 2, 2023
1 parent 344dfe1 commit 8fea33c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
40 changes: 24 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
# mazecpc 1.1
Maze CPC is a maze generator in 14 bytes for Amstrad CPC series 8-bit computers.

![screen shot](https://raw.githubusercontent.com/ssg/mazecpc/master/screenshot.png)

# history
There is a book called "[10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10](http://10print.org/)". Yes that's the title. It's about a small code piece in BASIC language that generates a maze. An interesting read for programmers. It delves into attempts to create it in assembly as small as possible, which [seems to have become a trend](http://csdb.dk/release/?id=113300) in Commodore 64 demoscene.

Recently I saw [a blog post from Trixter](http://trixter.oldskool.org/2012/12/17/maze-generation-in-thirteen-bytes/) who tried to port it to PC. It eventually got down to 10 bytes.

Tonight I wanted to give it a shot on Amstrad CPC and here it is, <s>11 bytes</s>14-bytes maze generator, created in couple of hours.

# technical notes
I found it very hard to produce a series of "random bits" on a Z80 CPU. There is R register which increases every cycle but naturally deterministic in loops. To keep the code short, however, I relied on it and interrupts to introduce a slight entropy. I'm sure there is room for improvement. Update Dec 29th, 2018: I made the random generator much better by sacrificing 3 more bytes. It's a 14-byte maze generator now.

SSG / arteffect, March 24th 2015
# mazecpc 1.2

Maze CPC is a maze generator in 12 bytes for Amstrad CPC664/6128 8-bit computers.

![screen shot](https://raw.githubusercontent.com/ssg/mazecpc/master/screenshot.png)

# history

There is a book called "[10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10](http://10print.org/)". Yes that's the title. It's about a small code piece in BASIC language that generates a maze. An interesting read for programmers. It delves into attempts to create it in assembly as small as possible, which [seems to have become a trend](http://csdb.dk/release/?id=113300) in Commodore 64 demoscene.

I saw [a blog post from Trixter](http://trixter.oldskool.org/2012/12/17/maze-generation-in-thirteen-bytes/) who tried to port it to PC. It eventually got down to 10 bytes.

Tonight I wanted to give it a shot on Amstrad CPC and here it is, <s>11</s> <s>14</s> 12-bytes maze generator, created in a couple of hours.

# technical notes

I found it very hard to produce a series of "random bits" on a Z80 CPU. There is R register which increases every cycle but naturally deterministic in loops. To keep the code short, however, I relied on it and interrupts to introduce a slight entropy. I'm sure there is room for improvement.

_Update Dec 29th, 2018: I made the random generator much better by sacrificing 3 more bytes. It's a 14-byte maze generator now._

_Update May 2nd, 2023: I resorted to using firmware call for RND to get the random number,
saving another 2 bytes. But, the code doesn't run on CPC464 now due to missing firmware call. The 14-byte CPC464 version still exists in `maze464.asm`_

SSG / arteffect, March 24th 2015
45 changes: 27 additions & 18 deletions maze.asm
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
org &4000
write direct "maze.bin"

CHAR equ &cc
TXT_WR_CHAR equ &bb5d

run $

.loop:
ld a, r
rrca
rrca
rrca
and 1
or CHAR
call TXT_WR_CHAR
jr loop

; This is the 12-byte CPC664/CPC6128 version.

org &4000
write "maze.bin"
write direct "maze.bin"

CHAR equ &CC
TXT_WR_CHAR equ &BB5D
REAL_RND equ &BD7F ; CPC464 doesn't have this endpoint.

run $
loop:
; REAL_RND call normally works on the memory at HL register
; we ignore that and use the destroyed A value because somehow
; it also varies quite randomly, saving us 2 bytes in the end.
; This trick causes RND to output junk pixels to the scroll
; position due to HL value destroyed by TXT_WR_CHAR, but it
; gets painted over anyway.
call REAL_RND
and 1
or CHAR
call TXT_WR_CHAR
jr loop

result:

close
1 change: 0 additions & 1 deletion maze.bin
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
�_����]��
Binary file modified maze.dsk
Binary file not shown.
2 changes: 1 addition & 1 deletion winape.asm
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
; this is to avoid using winape's own editor
; this is to avoid using winape's own editor
read "maze.asm"

0 comments on commit 8fea33c

Please sign in to comment.