6502 preprocessor

2007-12-11 16:51:02

I have a prototype preprocessor for the 6502 screensaver working in lisp. I still need to add parameters for the macros and then translate it to c.

I came across an old book "Software Tools in Pascal" that has a whole chapter devoted to C style macro processing. The one from the book will probable be the bases for the final version. One thing that I am going to do different is to have two key words 'let' and 'define'. Let will be allowed on one line and define can span multiple line. Define is terminated with the keyword 'end'.

Here is an example:


;This is the bit that gets painted
let cell_lsb $0
let cell_msb $1

;Amount to increment the bit
let incr_lsb $2
let incr_msb $3

;The location of the color
let color    $4

;beginning of the frame buffer address
let fbb_lsb  #0
let fbb_msb  #2

;end of the frame buffer address
let fbe_lsb  #$ff
let fbe_msb  #5

;amount to increment the cell each time
let incr_amt_lsb #3
let incr_amt_msb #0

;built in random memory location
let rand $fe

define init
  lda fbb_lsb
  sta cell_lsb
  lda fbb_msb
  sta cell_msb
  lda incr_amt_lsb
  sta incr_lsb
  lda incr_amt_msb
  sta incr_msb
  lda rand
  sta color
end

define inc_cell
  clc
  lda cell_lsb
  adc incr_lsb
  sta cell_lsb
  lda cell_msb
  adc incr_msb
  sta cell_msb
end

define paint
  ldx #0
  lda color
  sta (cell_lsb,x)
end

;;Paint a random colored checkerboard on the screen.

cld
main_loop:
init
loop:
inc_cell
paint

;have we reached the end of the buffer?
;if so go back to the main loop
lda fbe_msb
cmp cell_msb
bne loop
lda fbe_lsb
cmp cell_lsb
bne loop
jmp main_loop

The above code expands to the following:


cld
main_loop: 

lda  #0
sta  $0
lda  #2
sta  $1
lda  #3
sta  $2
lda  #0
sta  $3
lda  $fe
sta  $4 

loop:
clc
lda  $0
adc  $2
sta  $0
lda  $1
adc  $3
sta  $1 

ldx #0
lda  $4
sta ($0, x) 

;have we reached the end of the buffer?
;if so go back to the main loop
lda  #5
cmp  $1
bne loop
lda  #$ff
cmp  $0
bne loop
jmp main_loop

If you want to give it a try paste the expanded code at 6502asm.com or you can save it to a file and invoke the screensaver with m6502 -file foo.asm