-
Notifications
You must be signed in to change notification settings - Fork 2
/
proto-arc.asm
424 lines (345 loc) · 7.89 KB
/
proto-arc.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
; ============================================================================
; Prototype Framework - stripped back from stniccc-archie.
; ============================================================================
.equ _DEBUG, 1
.equ _ENABLE_MUSIC, 1
.equ _FIX_FRAME_RATE, 0 ; useful for !DDT breakpoints
.equ _SYNC_EDITOR, 1
.equ Screen_Banks, 3
.equ Screen_Mode, 9
.equ Screen_Width, 320
.equ Screen_Height, 256
.equ Window_Width, 256
.equ Window_Height, 200
.equ Screen_Stride, Screen_Width/2 ; 4bpp
.equ Screen_Bytes, Screen_Stride*Screen_Height
.equ Window_Stride, Screen_Width/2 ; 4bpp
.equ Window_Bytes, Window_Stride*Window_Height
.include "lib/swis.h.asm"
.org 0x8000
; ============================================================================
; Stack
; ============================================================================
Start:
adrl sp, stack_base
B main
.skip 1024
stack_base:
; ============================================================================
; Main
; ============================================================================
main:
MOV r0,#22 ;Set MODE
SWI OS_WriteC
MOV r0,#Screen_Mode
SWI OS_WriteC
; Set screen size for number of buffers
MOV r0, #DynArea_Screen
SWI OS_ReadDynamicArea
MOV r0, #DynArea_Screen
MOV r2, #Screen_Bytes * Screen_Banks
SUBS r1, r2, r1
SWI OS_ChangeDynamicArea
MOV r0, #DynArea_Screen
SWI OS_ReadDynamicArea
CMP r1, #Screen_Bytes * Screen_Banks
ADRCC r0, error_noscreenmem
SWICC OS_GenerateError
MOV r0,#23 ;Disable cursor
SWI OS_WriteC
MOV r0,#1
SWI OS_WriteC
MOV r0,#0
SWI OS_WriteC
SWI OS_WriteC
SWI OS_WriteC
SWI OS_WriteC
SWI OS_WriteC
SWI OS_WriteC
SWI OS_WriteC
SWI OS_WriteC
; LOAD STUFF HERE!
.if _ENABLE_MUSIC
; Load module
adrl r0, module_filename
mov r1, #0
swi QTM_Load
mov r0, #48
swi QTM_SetSampleSpeed
.endif
; Clear all screen buffers
mov r1, #1
.1:
str r1, scr_bank
; CLS bank N
mov r0, #OSByte_WriteVDUBank
swi OS_Byte
mov r0, #12
SWI OS_WriteC
ldr r1, scr_bank
add r1, r1, #1
cmp r1, #Screen_Banks
ble .1
; Start with bank 1
mov r1, #1
str r1, scr_bank
; Claim the Error vector
MOV r0, #ErrorV
ADR r1, error_handler
MOV r2, #0
SWI OS_Claim
; Claim the Event vector
mov r0, #EventV
adr r1, event_handler
mov r2, #0
swi OS_AddToVector
; LATE INITALISATION HERE!
; Sync tracker.
bl rocket_init
bl rocket_start
; Enable Vsync event
mov r0, #OSByte_EventEnable
mov r1, #Event_VSync
SWI OS_Byte
main_loop:
; Block if we've not even had a vsync since last time - we're >50Hz!
ldr r1, last_vsync
.1:
ldr r2, vsync_count
cmp r1, r2
beq .1
.if _FIX_FRAME_RATE
mov r0, #1
.else
sub r0, r2, r1
.endif
str r2, last_vsync
; R0 = vsync delta since last frame.
bl rocket_update
; show debug
.if _DEBUG
bl debug_write_vsync_count
.endif
; DO STUFF HERE!
bl get_next_screen_for_writing
mov r0, #0
bl rocket_sync_get_val_hi
bl show_screen_at_vsync
; exit if Escape is pressed
MOV r0, #OSByte_ReadKey
MOV r1, #IKey_Escape
MOV r2, #0xff
SWI OS_Byte
CMP r1, #0xff
CMPEQ r2, #0xff
BEQ exit
b main_loop
error_noscreenmem:
.long 0
.byte "Cannot allocate screen memory!"
.align 4
.long 0
.if _DEBUG
debug_write_vsync_count:
mov r0, #30
swi OS_WriteC
.if _ENABLE_MUSIC
; read current tracker position
mov r0, #-1
mov r1, #-1
swi QTM_Pos
mov r3, r1
adr r1, debug_string
mov r2, #8
swi OS_ConvertHex2
adr r0, debug_string
swi OS_WriteO
mov r0, r3
adr r1, debug_string
mov r2, #8
swi OS_ConvertHex2
adr r0, debug_string
swi OS_WriteO
.else
ldr r0, rocket_sync_time
adr r1, debug_string
mov r2, #8
swi OS_ConvertHex4
adr r0, debug_string
swi OS_WriteO
.endif
mov pc, r14
debug_string:
.skip 8
.endif
get_screen_addr:
str lr, [sp, #-4]!
adrl r0, screen_addr_input
adrl r1, screen_addr
swi OS_ReadVduVariables
ldr pc, [sp], #4
screen_addr_input:
.long VD_ScreenStart, -1
screen_addr:
.long 0 ; ptr to the current VIDC screen bank being written to.
exit:
; wait for vsync (any pending buffers)
mov r0, #19
swi OS_Byte
.if _ENABLE_MUSIC
; disable music
mov r0, #0
swi QTM_Stop
.endif
; disable vsync event
mov r0, #OSByte_EventDisable
mov r1, #Event_VSync
swi OS_Byte
; release our event handler
mov r0, #EventV
adr r1, event_handler
mov r2, #0
swi OS_Release
; release our error handler
mov r0, #ErrorV
adr r1, error_handler
mov r2, #0
swi OS_Release
; Display whichever bank we've just written to
mov r0, #OSByte_WriteDisplayBank
ldr r1, scr_bank
swi OS_Byte
; and write to it
mov r0, #OSByte_WriteVDUBank
ldr r1, scr_bank
swi OS_Byte
SWI OS_Exit
; R0=event number
event_handler:
cmp r0, #Event_VSync
movnes pc, r14
STMDB sp!, {r0-r1, lr}
; update the vsync counter
LDR r0, vsync_count
ADD r0, r0, #1
STR r0, vsync_count
; is there a new screen buffer ready to display?
LDR r1, buffer_pending
CMP r1, #0
LDMEQIA sp!, {r0-r1, pc}
; set the display buffer
MOV r0, #0
STR r0, buffer_pending
MOV r0, #OSByte_WriteDisplayBank
; some SVC stuff I don't understand :)
STMDB sp!, {r2-r12}
MOV r9, pc ;Save old mode
ORR r8, r9, #3 ;SVC mode
TEQP r8, #0
MOV r0,r0
STR lr, [sp, #-4]!
SWI XOS_Byte
; set full palette if there is a pending palette block
ldr r2, palette_pending
cmp r2, #0
beq .4
adr r1, palette_osword_block
mov r0, #16
strb r0, [r1, #1] ; physical colour
mov r3, #0
.3:
strb r3, [r1, #0] ; logical colour
ldr r4, [r2], #4 ; rgbx
and r0, r4, #0xff
strb r0, [r1, #2] ; red
mov r0, r4, lsr #8
strb r0, [r1, #3] ; green
mov r0, r4, lsr #16
strb r0, [r1, #4] ; blue
mov r0, #12
swi XOS_Word
add r3, r3, #1
cmp r3, #16
blt .3
mov r0, #0
str r0, palette_pending
.4:
LDR lr, [sp], #4
TEQP r9, #0 ;Restore old mode
MOV r0, r0
LDMIA sp!, {r2-r12}
LDMIA sp!, {r0-r1, pc}
; TODO: rename these to be clearer.
scr_bank:
.long 0 ; current VIDC screen bank being written to.
palette_block_addr:
.long 0 ; (optional) ptr to a block of palette data for the screen bank being written to.
vsync_count:
.long 0 ; current vsync count from start of exe.
last_vsync:
.long 0 ; vsync count at start of previous frame.
buffer_pending:
.long 0 ; screen bank number to display at vsync.
palette_pending:
.long 0 ; (optional) ptr to a block of palette data to set at vsync.
error_handler:
STMDB sp!, {r0-r2, lr}
MOV r0, #OSByte_EventDisable
MOV r1, #Event_VSync
SWI OS_Byte
MOV r0, #EventV
ADR r1, event_handler
mov r2, #0
SWI OS_Release
MOV r0, #ErrorV
ADR r1, error_handler
MOV r2, #0
SWI OS_Release
MOV r0, #OSByte_WriteDisplayBank
LDR r1, scr_bank
SWI OS_Byte
LDMIA sp!, {r0-r2, lr}
MOVS pc, lr
show_screen_at_vsync:
; Show current bank at next vsync
ldr r1, scr_bank
str r1, buffer_pending
; Including its associated palette
ldr r1, palette_block_addr
str r1, palette_pending
mov pc, lr
get_next_screen_for_writing:
; Increment to next bank for writing
ldr r1, scr_bank
add r1, r1, #1
cmp r1, #Screen_Banks
movgt r1, #1
str r1, scr_bank
; Now set the screen bank to write to
mov r0, #OSByte_WriteVDUBank
swi OS_Byte
; Back buffer address for writing bank stored at screen_addr
b get_screen_addr
; ============================================================================
; Additional code modules
; ============================================================================
.include "lib/rocket.asm"
; ============================================================================
; Data Segment
; ============================================================================
.if _ENABLE_MUSIC
module_filename:
.byte "<Demo$Dir>.Music",0
.align 4
.endif
; ============================================================================
; BSS Segment
; ============================================================================
palette_osword_block:
.skip 8
; logical colour
; physical colour (16)
; red
; green
; blue
; (pad)