-
Notifications
You must be signed in to change notification settings - Fork 0
/
256-beeb.asm
367 lines (307 loc) · 7.22 KB
/
256-beeb.asm
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
\ ******************************************************************
\ * 256b demo framework
\ ******************************************************************
_DEBUG = TRUE ; don't limit to 256b
_STABLE_RASTER = FALSE
_DEMO_SIZE = 256
\ ******************************************************************
\ * OS defines
\ ******************************************************************
osfile = &FFDD
oswrch = &FFEE
osasci = &FFE3
osbyte = &FFF4
osword = &FFF1
osfind = &FFCE
osgbpb = &FFD1
osargs = &FFDA
\\ Palette values for ULA
PAL_black = (0 EOR 7)
PAL_blue = (4 EOR 7)
PAL_red = (1 EOR 7)
PAL_magenta = (5 EOR 7)
PAL_green = (2 EOR 7)
PAL_cyan = (6 EOR 7)
PAL_yellow = (3 EOR 7)
PAL_white = (7 EOR 7)
ULA_Mode2 = &F4
\ ******************************************************************
\ * SYSTEM defines
\ ******************************************************************
MODE1_COL0=&00
MODE1_COL1=&20
MODE1_COL2=&80
MODE1_COL3=&A0
\ ******************************************************************
\ * MACROS
\ ******************************************************************
MACRO WAIT_NOPS n
PRINT "WAIT",n," CYCLES AS NOPS"
IF n < 0
ERROR "Can't wait negative cycles!"
ELIF n=0
; do nothing
ELIF n=1
EQUB $33
PRINT "1 cycle NOP is Master only and not emulated by b-em."
ELIF (n AND 1) = 0
FOR i,1,n/2,1
NOP
NEXT
ELSE
BIT 0
IF n>3
FOR i,1,(n-3)/2,1
NOP
NEXT
ENDIF
ENDIF
ENDMACRO
MACRO WAIT_CYCLES n
PRINT "WAIT",n," CYCLES"
IF n >= 12
FOR i,1,n/12,1
JSR return
NEXT
WAIT_NOPS n MOD 12
ELSE
WAIT_NOPS n
ENDIF
ENDMACRO
\ ******************************************************************
\ * GLOBAL constants
\ ******************************************************************
; Default screen address
screen_addr = &3000
SCREEN_SIZE_BYTES = &8000 - screen_addr
row_bytes = 640
; Exact time for a 50Hz frame less latch load time
FramePeriod = 312*64-2
; Calculate here the timer value to interrupt at the desired line
TimerValue = 32*64 - 2*64 - 2 - 25 + 40
\\ 40 lines for vblank
\\ 32 lines for vsync (vertical position = 35 / 39)
\\ interupt arrives 2 lines after vsync pulse
\\ 2 us for latch
\\ XX us to fire the timer before the start of the scanline so first colour set on column -1
\\ YY us for code that executes after timer interupt fires
\ ******************************************************************
\ * ZERO PAGE
\ ******************************************************************
ORG &70
GUARD &9F
.u skip 1
.v skip 1
\ ******************************************************************
\ * CODE START
\ ******************************************************************
ORG &1C05
IF _DEBUG
GUARD screen_addr ; ensure code size doesn't hit start of screen memory
ELSE
GUARD &1C05+_DEMO_SIZE
ENDIF
.start
\ ******************************************************************
\ * Code entry
\ ******************************************************************
.main_start
.main
{
\\ Set MODE
lda #22:jsr oswrch
lda #2:jsr oswrch
\\ Turn off interlace & cursor
lda #8:sta &fe00
lda #&c0:sta &fe01
\\ Init vars.
lda #0
sta u
sta v
\\ Set up screen.
IF 1
{
.outer
ldx #7
.loop1
ldy #7
.loop2
lda pixel_pairs, X
.write_to
sta screen_addr
inc write_to+1
bne ok
inc write_to+2
bmi done
.ok
dey
bpl loop2
dex
bpl loop1
bmi outer
.done
}
ENDIF
\\ Disable interrupts
sei
\\ Wait for vsync
{
lda #2
.vsync1
bit &FE4D
beq vsync1
}
; Roughly synced to VSync
IF _STABLE_RASTER
; Now fine tune by waiting just less than one frame
; and check if VSync has fired. Repeat until it hasn't.
; One frame = 312*128 = 39936 cycles
{
.syncloop
STA &FE4D ; 6
LDX #209 ; 2
.outerloop
LDY #37 ; 2
.innerloop
DEY ; 2
BNE innerloop ; 3/2 (innerloop = 5*37+2-1 = 186)
DEX ; 2
BNE outerloop ; 3/2 (outerloop = (186+2+3)*209+2-1 = 39920)
BIT &FE4D ; 6
BNE syncloop ; 3 (total = 39920+6+6+3 = 39935, one cycle less than a frame!)
IF HI(syncloop) <> HI(P%)
ERROR "This loop must execute within the same page"
ENDIF
}
; We are synced precisely with VSync!
ENDIF
\\ Set up Timer1 to start at the first scanline
LDA #LO(TimerValue):STA &FE44 ; 8c
LDA #HI(TimerValue):STA &FE45 ; 8c
; Latch T1 to interupt exactly every 50Hz frame
LDA #LO(FramePeriod):STA &FE46 ; 8c
LDA #HI(FramePeriod):STA &FE47 ; 8c
\ ******************************************************************
\ * MAIN LOOP
\ ******************************************************************
.frame_loop
\\ Do any processing in vblank here!
inc u
inc v
\\ Set vertical colours
{
lda #0
sta ok+1
ldx u
.loop
txa
and #8
bne ok
lda #15
.ok
ora #0
sta &fe21
inx
clc
lda ok+1
adc #&10
sta ok+1
bcc loop
}
\\ Synchronise with start of screen display.
{
LDA #&40
.waitTimer1
BIT &FE4D
BEQ waitTimer1
\\ Reading the T1 low order counter also resets the T1 interrupt flag in IFR
LDA &FE44
IF _STABLE_RASTER
\\ New stable raster NOP slide thanks to VectorEyes 8)
\\ Observed values $FA (early) - $F7 (late) so map these from 7 - 0
\\ then branch into NOPs to even this out.
AND #15
SEC
SBC #7
EOR #7
STA branch+1
.branch
BNE branch
NOP
NOP
NOP
NOP
NOP
NOP
NOP
.stable
ENDIF
\\ Now synced to scanline 0, character 0.
}
\\ Effect here!
ldy #0 ; 2c
ldx v
.scanline_loop
{
txa ; 2c
and #16 ; 2c
lsr a:lsr a:lsr a:lsr a ; 8c
ora #&f4 ; 2c
sta &fe20 ; 4c
inx ; 2c
WAIT_CYCLES 123-20
iny ; 2c
bne scanline_loop ; 3c
; loop total = 128c
}
\\ Use branch to save a byte.
jmp frame_loop
}
.return
rts
.main_end
\ ******************************************************************
\ * SYSTEM DATA
\ ******************************************************************
.data_start
\\ pixel pairs
.pixel_pairs
equb %11111101 ; 14, 15
equb %11110001 ; 12, 13
equb %11001101 ; 10, 11
equb %11000001 ; 8, 9
equb %00111101 ; 6, 7
equb %00110001 ; 4, 5
equb %00001101 ; 2, 3
equb %00000001 ; 0, 1
.data_end
\ ******************************************************************
\ * End address to be saved
\ ******************************************************************
.end
\ ******************************************************************
\ * Save the code
\ ******************************************************************
SAVE "256beeb", start, end, main
\ ******************************************************************
\ * Space reserved for runtime buffers not preinitialised
\ ******************************************************************
ALIGN &100
.bss_start
.bss_end
\ ******************************************************************
\ * Memory Info
\ ******************************************************************
PRINT "------"
PRINT "256b demo"
PRINT "------"
PRINT "CODE size =", ~main_end-main_start
PRINT "DATA size =",~data_end-data_start
PRINT "BSS size =",~bss_end-bss_start
PRINT "------"
PRINT "HIGH WATERMARK =", ~P%
PRINT "FREE =", ~start+_DEMO_SIZE-end
PRINT "------"
\ ******************************************************************
\ * Any other files for the disc
\ ******************************************************************