-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakehourglass.py
executable file
·613 lines (545 loc) · 28.7 KB
/
makehourglass.py
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#!/usr/bin/env python
"""
Make the data for a set of hourglass calls.
We take the shape.py data and we produce some code that can be used to use the hourglass.
"""
import os
import pprint
import sys
# We want to use the 'shape.py' from the working directory
sys.path.append('.')
import shape
# Enable this to write the row data directly for each frame, rather than using
# a list of rowdata offsets.
# This is only efficient on memory if the rows are common between the different
# hourglass frames, otherwise it means that we're writing more data than we need
# in the module. But is that really an issue?
# Turns out that it is - the fact that a row isn't (usually 2 words) and we have
# to spend an extra word to reference it means that you need to have a lot of
# duplicated rows to make these references to rows efficient.
# Sadly I've optimised for a case that isn't likely, or even possible, on
# RISC OS Classic. As such, the direct_rowdata is left on.
# If you were to have a repetative hourglass which was wider than 2 words,
# you might turn this off again, but it doesn't offer any benefit right now.
direct_rowdata = True
# Number of bits per pixel
bpp = 2
# Number of pixels per word
ppw = 32 / bpp
def rowstring_to_words(rowstring):
words = []
word = 0
for index, char in enumerate(rowstring):
wordpixel = index & (ppw-1)
word = word | (int(char) << (wordpixel * bpp))
if wordpixel == (ppw-1) or index == len(rowstring) - 1:
# This is the last pixel of the word, or the last pixel of the string
# So we write into the array.
words.append(word)
word = 0
return words
# Construct our unique row data.
# rows contains a mapping from the shape string to the index of the data for that row
# rowdata contains the actual data (at this point, not converted to words)
rows = {}
rowdata = []
# images_rowindexes will be constructed to contain a list of indexes into that rowdata
# for every row of every image.
images_rowindexes = []
for image in shape.images:
imagerows = []
for rowstring in image:
if rowstring in image:
if rowstring in rows:
# This is a row we've seen before, so we can reuse it
rowindex = rows[rowstring]
else:
# We've not seen this row before, so we will create a new entry for it
rowindex = len(rowdata)
rows[rowstring] = rowindex
rowdata.append(rowstring)
imagerows.append(rowindex)
images_rowindexes.append(imagerows)
# Convert the string rowdata into words to write into memory
rowdata = [rowstring_to_words(rowstring) for rowstring in rowdata]
# Construct a list of deltas from the previous image - this is a list of tuples of (rownumber, rowindex)
# Strictly we could probably just store this data raw in memory and just reference it directly, or
# even copy the lot every time. But there's a certain amount of elegance in compacting the data in this
# way that I rather like.
deltas = []
# The first entry will always be a complete write of the image data, because it's the first frame -
# so we want to start with a clean slate each time.
deltas.append([(index, rowindex) for index, rowindex in enumerate(images_rowindexes[0])])
# Take a copy of the first image's state, so that we can track as calculate the deltas for each frame.
current_state = images_rowindexes[0][:]
# Now for each subsequent frame, we work out the deltas and store them into the array.
for rowindexes in images_rowindexes[1:]:
image_deltas = []
for index, rowindex in enumerate(rowindexes):
if current_state[index] != rowindex:
# This row differs from the previous frame, so we write its delta.
image_deltas.append((index, rowindex))
current_state[index] = rowindex
deltas.append(image_deltas)
#pprint.pprint(images_rowindexes)
#pprint.pprint(rowdata)
#pprint.pprint(deltas[0])
def make_basic(rows, rowdata, deltas, images_rowindexes, filename):
"""
Write out a BASIC program that includes the data we wish to use.
"""
lines = []
# Construct the rowdata
wordsperrow = len(rowdata[0])
nwords = wordsperrow * len(rowdata)
# This algorithm only supports widths that are multiples of words
lines.append("ON ERROR ERROR EXT ERR, REPORT$+\" at line \"+STR$ERL")
lines.append(":")
lines.append("COLOUR 128+7:CLS:COLOUR0")
lines.append(":")
lines.append("width% = {}".format((shape.width + 15) & ~15))
lines.append("height% = {}".format(shape.height))
lines.append("active_x% = {}".format(shape.activex))
lines.append("active_y% = {}".format(shape.activey))
lines.append("period% = {}".format(shape.frameperiod))
lines.append("wordsperrow% = {}".format(wordsperrow))
lines.append("nwords% = {}".format(nwords))
lines.append("DIM rowdata% 4 * nwords%")
lines.append("RESTORE +1")
def basic_int(word):
#if word & (1<<31):
# word = -(word - (1<<32))
# return '-&{:08x}'.format(word)
return '&{:08x}'.format(word)
for words in rowdata:
words = [basic_int(word) for word in words]
lines.append("DATA {}".format(', '.join(words)))
lines.append("FOR n% = 0 TO nwords%-1")
lines.append(" READ word%:rowdata%!(4 * n%) = word%")
lines.append("NEXT")
lines.append(":")
# Set aside space for the frame references
lines.append("nframes% = {}".format(len(deltas)))
lines.append("DIM framedata% 8 * nframes%")
lines.append(":")
# Construct the list of deltas, and populate the frame references
deltasize = sum(len(image_deltas) + 1 for image_deltas in deltas)
lines.append("DIM deltas% 8*{}".format(deltasize))
lines.append("")
lines.append("deltaoffset% = 0")
lines.append("RESTORE +1")
lines.append("FOR frame% = 0 TO nframes%-1")
lines.append(" framedata%!(frame% * 4) = deltaoffset%")
lines.append(" REPEAT")
lines.append(" READ rownumber%, rowindex%")
# In theory this could be two halfwords, because the data is so small
lines.append(" deltas%!(deltaoffset%) = rownumber%")
lines.append(" deltas%!(deltaoffset%+4) = rowindex%")
lines.append(" deltaoffset% += 8")
lines.append(" UNTIL rownumber% = -1")
lines.append("NEXT")
lines.append(":")
# Here's the actual delta data
for index, image_deltas in enumerate(deltas):
lines.append("REM Image {}".format(index))
for rownumber, rowindex in image_deltas:
lines.append("DATA {}, {}".format(rownumber, rowindex))
lines.append("DATA {}, {}".format(-1, -1))
lines.append(":")
lines.append("DIM worddata% 10")
lines.append("DIM pointerdata% 4 * wordsperrow% * height%")
lines.append("worddata%?0 = 0:REM Define pointer shape")
lines.append("worddata%?1 = 1:REM Shape number")
lines.append("worddata%?2 = (width%*2+7)/8:REM Width in bytes")
lines.append("worddata%?3 = height%:REM Height in rows")
lines.append("worddata%?4 = active_x%")
lines.append("worddata%?5 = active_y%")
lines.append("worddata%!6 = pointerdata%")
lines.append(":")
lines.append("RESTORE + 1")
lines.append("FOR col% = 0 TO {}".format(len(shape.palette)-1))
lines.append(" READ colr%,colg%,colb%")
lines.append(" VDU 19, col%, 25, colr%, colg%, colb%")
lines.append("NEXT")
for cols in shape.palette:
lines.append("DATA {}".format(', '.join(str(col) for col in cols)))
lines.append(":")
# Select the pointer and turn it on
lines.append("SYS \"OS_Byte\", 106, 1")
lines.append(":")
# Now we can step through the deltas for each frame.
lines.append("framenum%=0")
lines.append("WHILE TRUE")
lines.append(" frame_deltas% = deltas% + framedata%!(framenum% * 4)")
lines.append(" PRINT \"Frame \";framenum%")
lines.append(" REPEAT")
lines.append(" rownumber%=frame_deltas%!0:frame_deltas%+=4")
lines.append(" rowindex%=frame_deltas%!0:frame_deltas%+=4")
#lines.append(" PRINT \" row \";rownumber%;\" => index \";rowindex%")
lines.append(" IF rownumber% <> -1 THEN")
# This would probably just be hardcoded for a specific case in a module - the whole point of using the
# deltas is to not waste time, so looping is very wasteful.
lines.append(" FORoffset%=0TOwordsperrow%*4-4 STEP 4")
lines.append(" pointerdata%!(rownumber% * 4 * wordsperrow% + offset%) = rowdata%!(4 * wordsperrow% * rowindex% + offset%)")
lines.append(" NEXT")
lines.append(" ENDIF")
lines.append(" UNTIL rownumber%=-1")
lines.append(" SYS \"OS_Word\",21, worddata%")
lines.append(" framenum% = (framenum% + 1) MOD nframes%")
lines.append(" I=INKEY(period%)")
lines.append("ENDWHILE")
if sys.platform != 'riscos':
filename += ',fd1'
with open(filename, 'w') as fh:
for line in lines:
fh.write("{}\n".format(line))
if sys.platform == 'riscos':
# FIXME: On RISC OS set the filetype
pass
def make_objasm(rows, rowdata, deltas, images_rowindexes,filename):
# Write out an objasm function to allow us to run the hourglass,
# and the C header that lets us use it
lines = []
hlines = []
# Header for the H file
hlines.append("#ifndef HOURGLASS_ASM_H")
hlines.append("#define HOURGLASS_ASM_H")
hlines.append("")
hlines.append("typedef void *hourglass_workspace_t;")
# Construct the rowdata
wordsperrow = len(rowdata[0])
nwords = wordsperrow * len(rowdata)
# This algorithm only supports widths that are multiples of words
lines.append(" AREA |C$$code|, CODE, READONLY")
lines.append("")
lines.append("; constants")
lines.append("width * {}".format((shape.width + 15) & ~15))
lines.append("height * {}".format(shape.height))
lines.append("period * {}".format(shape.frameperiod))
lines.append("wordsperrow * {}".format(wordsperrow))
lines.append("nwords * {}".format(nwords))
lines.append("nframes * {}".format(len(deltas)))
lines.append("active_x * {}".format(shape.activex))
lines.append("active_y * {}".format(shape.activey))
lines.append("")
lines.append("; Workspace for changing the hourglass rendition")
lines.append(" ^ 0, r12")
lines.append("hg_framenum # 4 ; frame number")
lines.append("hg_percentage # 4 ; percentage to show (N/I)")
lines.append("hg_leds # 4 ; LED flags (N/I)")
lines.append("hg_oldpointer # 4 ; Old pointer configuration")
lines.append("hg_oldcolours # 4 * 3 ; Old palette entries")
lines.append("hg_word # 12 ; OS_Word block")
lines.append("hg_currentdata # 4 * wordsperrow * height ; Current data for the hourglass")
lines.append("hg_workspacesize * :INDEX: @ ; Size of this workspace")
lines.append("")
lines.append("; SWI numbers")
lines.append("XOS_Byte * 0x20006")
lines.append("XOS_Word * 0x20007")
lines.append("XOS_ReadPalette * 0x2002F")
lines.append("")
lines.append('; -------------------------------------------------')
lines.append(' MACRO')
lines.append('$label SIGNATURE')
lines.append(' ALIGN 4')
lines.append(' = "$label",0')
lines.append(' ALIGN 4')
lines.append(' DCD &FF000000+(:LEN:"$label"+4):AND::NOT:3')
lines.append('$label')
lines.append(' MEND')
lines.append('')
if not direct_rowdata:
lines.append("; Data for the rows of the hourglass")
lines.append("rowdata")
for words in rowdata:
words = ['&{:08x}'.format(word) for word in words]
lines.append(" DCD {}".format(', '.join(words)))
framedelta_offsets = []
deltaoffset = 0
lines.append("")
lines.append("; Data for deltas between frames")
lines.append("deltas")
# Here's the actual delta data
for index, image_deltas in enumerate(deltas):
framedelta_offsets.append(deltaoffset)
lines.append(" ; frame {}".format(index))
for rownumber, rowindex in image_deltas:
# We actually write the offset into the hg_currentdata, and the offset into the rowdata
# This means that we don't need to calculate these values at runtime.
if direct_rowdata:
words = [rownumber * wordsperrow * 4]
words.extend(rowdata[rowindex])
lines.append(" DCD {}".format(', '.join('&{:08x}'.format(word) for word in words)))
deltaoffset += 4 * len(words)
else:
lines.append(" DCD {}, {}".format(rownumber * wordsperrow * 4,
rowindex * wordsperrow * 4))
deltaoffset += 8
lines.append(" DCD {}".format(-1))
deltaoffset += 4
lines.append("")
lines.append("; Offsets into the deltas for each frame")
lines.append("frame_deltas")
for index, offset in enumerate(framedelta_offsets):
lines.append(" DCD {} ; frame {}".format(offset, index))
lines.append("")
lines.append("; Palette data")
lines.append("palette")
# We skip the 0 entry, because colour 0 is transparent
for r, g, b in shape.palette[1:]:
lines.append(" DCB {}, {}, {}".format(r, g, b))
lines.append(" ALIGN")
lines.append("")
lines.append("; Read the period between frames")
lines.append("; <= R0 = cs between frames")
hlines.append("int hourglass_getframeperiod(void);")
lines.append("hourglass_getframeperiod SIGNATURE")
lines.append(" EXPORT hourglass_getframeperiod")
lines.append(" MOV r0, #period")
lines.append(" MOV pc, lr")
lines.append("")
lines.append("")
lines.append("; Read the size of the workspace block")
lines.append("; <= R0 = size of hourglass workspace")
hlines.append("int hourglass_getwssize(void);")
lines.append("hourglass_getwssize SIGNATURE")
lines.append(" EXPORT hourglass_getwssize")
lines.append(" MOV r0, #hg_workspacesize")
lines.append(" MOV pc, lr")
lines.append("")
lines.append("; Initialisation function")
lines.append("; => R0 = hourglass workspace")
hlines.append("void hourglass_init(hourglass_workspace_t *ws);")
lines.append("hourglass_init SIGNATURE")
lines.append(" EXPORT hourglass_init")
lines.append(" MOV r12, r0")
lines.append(" MOV r0, #0")
lines.append(" STR r0, hg_framenum")
lines.append(" STR r0, hg_percentage")
lines.append(" STR r0, hg_leds")
lines.append(" LDR r0, wordblock_0")
lines.append(" STR r0, hg_word")
lines.append(" LDR r0, wordblock_4")
lines.append(" ADR r1, hg_currentdata")
lines.append(" ORR r0, r0, r1, LSL #16 ; assign the low half word of pointer data")
lines.append(" STR r0, hg_word + 4")
lines.append(" MOV r0, r1, LSR #16 ; and the high half word")
lines.append(" STR r0, hg_word + 8")
lines.append("; no need to initialise the currentdata; it will be updated by the first frame")
lines.append(" MOV pc, lr")
lines.append("")
lines.append("wordblock_0")
lines.append(" DCB 0 ; define pointer shape")
lines.append(" DCB 3 ; shape number (will toggle 3-4)")
lines.append(" DCB (width*2+7)/8 ; width in bytes")
lines.append(" DCB height ; height")
lines.append("wordblock_4")
lines.append(" DCB active_x ; active x offset")
lines.append(" DCB active_y ; active y offset")
lines.append(" DCB 0 ; b0-7 of the address of the pointer data")
lines.append(" DCB 0 ; b8-15 of address of the pointer data")
lines.append("")
lines.append("; Start the hourglass")
lines.append("; => R0 = hourglass workspace")
hlines.append("void hourglass_start(hourglass_workspace_t *ws);")
lines.append("hourglass_start SIGNATURE")
lines.append(" EXPORT hourglass_start")
lines.append(" STMFD sp!, {r4, r5, lr}")
lines.append(" SUB sp, sp, #8")
lines.append(" MOV r12, r0")
lines.append(" MOV r0, #0")
lines.append(" STR r0, hg_framenum")
lines.append(" MOV r0, #1")
lines.append(" MOV r1, #25 ; read pointer colour 1")
lines.append(" SWI XOS_ReadPalette")
lines.append(" LDRVS r2, =&81397900 ; purple if it wasn't set")
lines.append(" STR r2, hg_oldcolours + 4 * 0")
lines.append(" MOV r0, #2")
lines.append(" MOV r1, #25 ; read pointer colour 2")
lines.append(" SWI XOS_ReadPalette")
lines.append(" LDRVS r2, =&66FFFF00 ; yellow if it wasn't set")
lines.append(" STR r2, hg_oldcolours + 4 * 1")
lines.append(" MOV r0, #3")
lines.append(" MOV r1, #25 ; read pointer colour 3")
lines.append(" SWI XOS_ReadPalette")
lines.append(" LDRVS r2, =&00000000 ; black if it wasn't set")
lines.append(" STR r2, hg_oldcolours + 4 * 2")
lines.append("; now set the palette up for our hourglass")
lines.append(" MOV r1, sp")
lines.append(" ADRL r2, palette")
for colour_number in range(len(shape.palette) - 1):
lines.append("; colour {}".format(colour_number + 1))
lines.append(" MOV r0, #{}".format(colour_number + 1))
lines.append(" STRB r0, [r1, #0]")
lines.append(" MOV r0, #25 ; set pointer colour 1")
lines.append(" STRB r0, [r1, #1]")
lines.append(" LDRB r0, [r2], #1")
lines.append(" STRB r0, [r1, #2] ; red")
lines.append(" LDRB r0, [r2], #1")
lines.append(" STRB r0, [r1, #3] ; green")
lines.append(" LDRB r0, [r2], #1")
lines.append(" STRB r0, [r1, #4] ; blue")
lines.append(" MOV r0, #12")
lines.append(" SWI XOS_Word ; Set palette")
lines.append(" MOV r1, #127 ; invalid pointer number to read the pointer")
lines.append(" MOV r0, #106 ; select pointer")
lines.append(" SWI XOS_Byte")
lines.append(" MOVVS r1, #0 ; if there was an error, turn off")
lines.append(" STR r1, hg_oldpointer")
lines.append(" ADD sp, sp, #8")
lines.append(" LDMFD sp!, {r4, r5, lr}")
lines.append(" MOV r0, r12")
lines.append(" B hourglass_frame ; exit via the first frame")
lines.append("")
lines.append("; Stop the hourglass")
lines.append("; => R0 = hourglass workspace")
hlines.append("void hourglass_stop(hourglass_workspace_t *ws);")
lines.append("hourglass_stop SIGNATURE")
lines.append(" EXPORT hourglass_stop")
lines.append(" STMFD sp!, {r4, r5, lr}")
lines.append(" SUB sp, sp, #8")
lines.append(" MOV r12, r0")
lines.append(" LDR r4, hg_oldpointer ; work out the old pointer shape")
lines.append(" BIC r4, r4, #127 ; turn off the pointer whilst we change colours")
lines.append(" MOV r0, #106 ; select pointer")
lines.append(" SWI XOS_Byte")
lines.append("; restore the palette up for old pointer")
lines.append(" MOV r1, sp")
for colour_number in range(len(shape.palette) - 1):
lines.append("; colour {}".format(colour_number + 1))
lines.append(" ADR r2, hg_oldcolours + 4 * {} + 1".format(colour_number))
lines.append(" MOV r0, #{}".format(colour_number + 1))
lines.append(" STRB r0, [r1, #0]")
lines.append(" MOV r0, #25 ; set pointer colour 1")
lines.append(" STRB r0, [r1, #1]")
lines.append(" LDRB r0, [r2], #1")
lines.append(" STRB r0, [r1, #2] ; red")
lines.append(" LDRB r0, [r2], #1")
lines.append(" STRB r0, [r1, #3] ; green")
lines.append(" LDRB r0, [r2], #1")
lines.append(" STRB r0, [r1, #4] ; blue")
lines.append(" MOV r0, #12")
lines.append(" SWI XOS_Word ; Set palette")
lines.append(" MOV r1, r4 ; re-select the old pointer number")
lines.append(" MOV r0, #106 ; select pointer")
lines.append(" SWI XOS_Byte")
lines.append(" ADD sp, sp, #8")
lines.append(" LDMFD sp!, {r4, r5, pc}")
lines.append("")
lines.append("; Frame update - sets the hourglass shape for the current frame")
lines.append("; => R0 = hourglass workspace")
hlines.append("void hourglass_frame(hourglass_workspace_t *ws);")
lines.append("hourglass_frame SIGNATURE")
lines.append(" EXPORT hourglass_frame")
lines.append(" STMFD sp!, {r4, r5, lr}")
lines.append(" MOV r12, r0")
lines.append(" LDR r0, hg_framenum")
lines.append(" ADRL r1, frame_deltas")
lines.append(" LDR r0, [r1, r0, LSL #2] ; offset within deltas for this frame")
lines.append(" ADRL r1, deltas")
lines.append(" ADD r1, r1, r0")
if not direct_rowdata:
lines.append(" ADRL r2, rowdata")
lines.append(" ADRL r5, hg_currentdata")
lines.append("rowloop")
if direct_rowdata:
lines.append(" LDR r3, [r1], #4 ; r3 = currentdata offset")
lines.append(" CMP r3, #-1 ; end of the rows")
lines.append(" BEQ rowend")
lines.append(" [ wordsperrow = 1")
lines.append(" LDR r0, [r1], #4 ; read a word")
lines.append(" STR r0, [r3, r5] ; store into the currentdata")
lines.append(" ]")
lines.append(" [ wordsperrow = 2")
lines.append(" LDMIA r1!, {r0, r2} ; read two word from rowdata")
lines.append(" STR r0, [r3, r5]! ; store into the currentdata")
lines.append(" STR r2, [r3, #4] ; store into the currentdata")
lines.append(" ]")
lines.append(" [ wordsperrow = 3")
lines.append(" LDMIA r1!, {r0, r2, r4} ; read a word from rowdata")
lines.append(" ADD r3, r3, r5 ; work out the line offset")
lines.append(" STMIA r3!, {r0, r2, r4} ; store into the currentdata")
lines.append(" ]")
lines.append(" [ wordsperrow = 4")
lines.append(" LDMIA r1!, {r0, r2, r4, lr} ; read a word from rowdata")
lines.append(" ADD r3, r3, r5 ; work out the line offset")
lines.append(" STMIA r3!, {r0, r2, r4, lr} ; store into the currentdata")
lines.append(" ]")
else:
lines.append(" LDMIA r1!, {r3, r4} ; r3 = currentdata offset, r4 = row data offset")
lines.append(" CMP r3, #-1 ; end of the rows")
lines.append(" BEQ rowend")
lines.append(" LDR r0, [r4, r2]! ; read a word from rowdata")
lines.append(" STR r0, [r3, r5]! ; store into the currentdata")
# FIXME: Remember how you do a loop in objasm, cos this could be pretty simple - alternatively use a
# LDMIA?
lines.append(" [ wordsperrow > 1")
lines.append(" LDR r0, [r4, #4]! ; read a word from rowdata")
lines.append(" STR r0, [r3, #4]! ; store into the currentdata")
lines.append(" ]")
lines.append(" [ wordsperrow > 2")
lines.append(" LDR r0, [r4, #4]! ; read a word from rowdata")
lines.append(" STR r0, [r3, #4]! ; store into the currentdata")
lines.append(" ]")
lines.append(" [ wordsperrow > 3")
lines.append(" LDR r0, [r4, #4]! ; read a word from rowdata")
lines.append(" STR r0, [r3, #4]! ; store into the currentdata")
lines.append(" ]")
lines.append(" B rowloop")
lines.append("")
lines.append("rowend")
# FIXME: Toggle the hourglass pointer number
lines.append(" MOV r0, #21 ; Set Pointer parameters")
lines.append(" ADR r1, hg_word")
lines.append(" SWI XOS_Word")
# It'd be better if we changed the pointer shape when we got the next VSync, but I'm being lazy.
lines.append("")
lines.append(" LDRB r1, hg_word + 1 ; get the hourglass pointer number we will use")
lines.append(" RSB lr, r1, #7 ; toggles 3 to 4")
lines.append(" STRB lr, hg_word + 1 ; toggles the pointer number we use next time")
lines.append(" MOV r0, #106 ; select pointer")
lines.append(" SWI XOS_Byte")
lines.append("")
lines.append(" LDR r0, hg_framenum")
lines.append(" ADD r0, r0, #1")
lines.append(" TEQ r0, #nframes")
lines.append(" MOVEQ r0, #0 ; reset counter; we've cycled")
lines.append(" STR r0, hg_framenum")
lines.append(" LDMFD sp!, {r4, r5, pc}")
lines.append("")
lines.append("; Frame update within an IRQ")
lines.append("; => R12 = hourglass workspace")
hlines.append("void hourglass_frame_irq(void);")
lines.append("hourglass_frame_irq SIGNATURE")
lines.append(" EXPORT hourglass_frame_irq")
lines.append(" STMFD sp!, {r0-r3, r4, r5, r12, lr}")
lines.append(" MOV r0, r12")
lines.append(" MRS r4, CPSR")
lines.append(" BIC r1, r4, #&F")
lines.append(" ORR r1, r1, #&3")
lines.append(" MSR CPSR_csxf, r1 ; Enter SVC mode (keep bitness)")
lines.append(" MOV r5, lr")
lines.append(" BL hourglass_frame")
lines.append(" MOV lr, r5")
lines.append(" MSR CPSR_cxsf, r4 ; Restore the mode")
lines.append(" LDMFD sp!, {r0-r3, r4, r5, r12, pc}")
lines.append("")
lines.append(" END")
# Trailer for the H file
hlines.append("")
hlines.append("#endif /* HOURGLASS_ASM_H */")
# Actually we'll build two files - one for the assembler s file, and one for the header
if not os.path.isdir('s'):
os.mkdir('s')
sfilename = os.path.join('s', filename)
with open(sfilename, 'w') as fh:
for line in lines:
fh.write("{}\n".format(line))
if not os.path.isdir('h'):
os.mkdir('h')
hfilename = os.path.join('h', filename)
with open(hfilename, 'w') as fh:
for line in hlines:
fh.write("{}\n".format(line))
make_basic(rows, rowdata, deltas, images_rowindexes, filename='hourglass_basic')
make_objasm(rows, rowdata, deltas, images_rowindexes, filename='asm')