-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.s
124 lines (85 loc) · 3.31 KB
/
main.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
org $8000
;; Install interrupt handler
int_addr: equ $fdfd
di
im 2
;; 1. copy fd everywhere in interrupt handler table at $fe00
ld hl,$fe00 ;Source = $fe00 (Start of vector table)
ld de,$fe01 ;Destination = $fe01
ld (hl),$fd ;Set first byte so it is copied to all of them ($fdfd)
ld bc,257 ;257 bytes (size of vector table)
ldir
;; 2. copy interrupt handler to address $fdfd
ld hl,int_copy ;Source = interrupt routine
ld de,int_addr ;Destination = $fdfd (as specified in vector table)
ld bc,int_end-int_start ;Length of custom interrupt routine
ldir ;(DE) <- (HL), BC=BC-1, Loop till B=0
ld a, $fe
ld i, a ; set interrupt register
ei
;; Setup videoUpdateList by null terminating the updates
setupVideoUpdateList:
ld a, $ff
ld (videoUpdateList), a
ld hl, videoUpdateList
ld (videoUpdateListCurr), hl ; set videoUpdateListCurr to point to beginning
;; Starting loop
include "startLoop.s"
;; This is the only thing called by the interrupt handler! We have X number of cycles to update video RAM
updateVideoRAM:
;; save regs on stack
push af
push bc
push hl
push de
push ix
;; UPDATE VIDEO RAM BEFORE CRT GETS TO SCREEN
ld hl, videoUpdateList
videoUpdateListLoop:
ld d, (hl) ; e = char cell row
ld a, d
cp $ff
jr z, videoUpdateListLoopEnd ; if we found a delimiter byte, stop parsing the list
inc hl
ld e, (hl) ; (e,d) = (row, col) for char cell
ld (charCellCoord), de ; load the coordinate to draw cell
; (keep in mind ZX Spectrum is little endian,
; so this will show up in memory as (row, col) )
inc hl ; hl now points to attribute byte
ld (attrByteAddress), hl ; load the attribute byte address
inc hl
ld a, (hl) ; hl now points to char cell ptr, do a deep copy
ld (charCellAddress), a ; (keep in mind ZX Spectrum is little endian)
inc hl
ld a, (hl)
ld (charCellAddress+1), a
push hl ;save hl cus the call will trash it
call copyCharCellAndAttrByteToScreen
pop hl
inc hl ; point to the next entry...
jp videoUpdateListLoop
videoUpdateListLoopEnd:
ld hl, videoUpdateList
ld (videoUpdateListCurr), hl ; set videoUpdateListCurr to point to beginning
;; null delimit the videoUpdateList
ld a, $ff
ld (videoUpdateList), a
;; restore regs from stack
pop ix
pop de
pop hl
pop bc
pop af
ei
reti
ret
include "spriteRoutines.s"
include "sprites.s"
;; The actual interrupt handler - it just jumps to our routine
;; (that way, we only need to copy 3 bytes to $fdfd and won't overwrite stuff at $fe00 -> $feff)
int_copy:
org int_addr ;Set instruction counter to $fdfd (interrupt handler)
int_start:
jp updateVideoRAM
int_end:
nop