Code: Select all
SCREEN = $5000
STACK = $0100
R0=$00 ;General pseduo registers
R1=$01
R2=$02
R3=$03
R4=$04
Test:
;Push arguments on to the stack
LDA #$20 ;X
PHA
LDA #$15 ;Y
PHA
LDA #$00 ;COLOR
PHA
JSR DrawPixel
;Pop the three arguments off
PLA
PLA
PLA
JMP Done
; Draws a pixel to the 64x64 screen
; Expects X Y COLOR on the stack
DrawPixel:
;Push the pseudo registers in case the calling
;function was using them
LDA R0
PHA
LDA R1
PHA
LDA R2
PHA
LDA R3
PHA
LDA R4
PHA
;Copy arguments from stack to registers
TSX
INX ;Skip current stack pointer address
INX ;Skip PC copy on stack
INX
INX ;Skip pseudo register copies
INX
INX
INX
INX
LDA STACK,X
STA R4 ;COLOR
INX
LDA STACK,X
STA R0 ;Y
INX
LDA STACK,X
STA R1 ;X
;Copy base address of video memory
;to R2-R3 which hold the final address
LDA #<SCREEN
STA R2
LDA #>SCREEN
STA R3
;Multiply Y times the width of the screen
CalcY:
LDA R0 ;Get Y argument
BEQ CalcX ;Is it 0 yet?
SEC
SBC #$01 ;Subtract 1 from Y
STA R0
LDA R2 ;Load low byte of final address
CLC
ADC #$40 ;Add screen width
STA R2
BCC CalcY ;If overflowed add one to high byte
LDA R3
INC
STA R3
JMP CalcY
;Add X to the final address
CalcX:
LDA R2 ;Load low byte of final address
CLC
ADC R1 ;Add X argument to it
STA R2
BCC Write ;If overflowed add one to high byte
LDA R3
INC
STA R3
Write:
LDA R4 ;Load color argument
LDY #$00
STA (R2),Y ;Write to final address
;Restore pseudo registers
PLA
STA R4
PLA
STA R3
PLA
STA R2
PLA
STA R1
PLA
STA R0
RTS
Done:
JMP Done
Code: Select all
#define SCREEN_WIDTH 0x40
SCREEN[Y * SCREEN_WIDTH + X] = color;