-
Notifications
You must be signed in to change notification settings - Fork 2
/
life.asm
594 lines (516 loc) · 11.6 KB
/
life.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
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
; -----------------------------------------------------------------------------
; Game of life created to illustrate my Turing Tarpit coding challenge.
; See http://en.wikipedia.org/wiki/Conway's_Game_of_Life and
; http://forum.6502.org/viewtopic.php?f=2&t=7262
;
; The goal of the challege is to use a reduced number of instructions as
; described below. The idea is to get a feel for what it would be like to
; program early machines such as the PDP-8.
;
; Martin Heermance <mheermance@gmail.com>
; -----------------------------------------------------------------------------
; Turing Tarpit Challege Overview
; The PDP-8 was tremendously influential to the early computer industry. It was
; cost engineered and Gordon Bell thought through the minimum number of viable
; instructions because it was built from descrete logic, so every gate counted.
; The resulting PDP-8 had eight memory instructions, an I/O instruction, NOP,
; and eight accumulator bit manipulation instructions.
;
; Like the 6502 the PDP-8's accumulator wasn't wide enough to hold an address,
; so the PDP-8 used pages with two addressing modes: PC and zero page. The
; 6502 has the latter, but the closet thing to PC page is PC relative and
; immediate mode. Because the PDP-8 was a word machine it didn't have variable
; length instructions to hold an absolute address as additional operands.
;
; The link register (carry bit) was the only processor status bit, so no zero,
; negative, or overflow bits and associated branch instructions. But there was
; a test for zero in the accumulator and branch instruction.
;
; So for the Turing Tarpit challenge I will restrict myself to the following
; 65c02 instructions and addressing modes.
;
; Accumulator operations: and, ora, eor, adc, clc, sec, inc, ror, rol, and asl
; Memory operations: lda #, lda absolute, lda (), sta absolute, and sta ()
; Flow control instructions: bcc, bcs, beq, bne, jmp absolute, jmp (),
; jsr absolute, rts, and nop
;
; That's 18 instructions which is a notable reduction from the 6502's 56, and
; matches the PDP-8. I'm allowing absolute addressing because the 6502 is a
; byte machine and one or two byte operands are intrinsic to such a design.
; I'm also allowing the zero control bit because the PDP-8 had skip on zero.
;
; Notable missing instructions are CMP, DEC and SBC. Subtraction is done by
; adding the two's complement, and precompute negative quantities. CMP is
; a nondestructive subtract. DEC is achieved by adding negative one. This was
; actually common on early machines with a discrete logic ALU.
;
; Aliases
;
; Character set (ASCII)
.alias AscBS $08 ; backspace ASCII character
.alias AscCC $03 ; break (Control-C) ASCII character
.alias AscCR $0D ; carriage return ASCII character
.alias AscDEL $7F ; DEL ASCII character
.alias AscESC $1B ; Escape ASCII character
.alias AscLF $0A ; line feed ASCII character
.alias AscSP $20 ; space ASCII character
.alias height 24 ; constants for board height, width, and size.
.alias width 32
.alias size height * width
.alias negOne $ffff ; precomputed negative quantities to avoid subtraction
.alias negSize [0-size]
.alias negWidth [0-width]
;
; Data segments
;
.data ZPDATA
.org $0080 ; we'll need to use ZP addressing
.space row 2 ; iterators for the row and column.
.space col 2
.space rowM 2 ; Row and column minus values
.space colM 2
.space rowP 2 ; Row and column plus values
.space colP 2
.space retval 2 ; variable for function return values.
.space temp 2 ; working variable
.space strPtr 2 ; pointer used for string processing.
.space rowJmpPtr 2 ; ponters to functions for iteration.
.space colJmpPtr 2
.space genCurr 2 ; ponters to buffers to allow gen swapping
.space genNext 2
.space genCurrPtr 2 ; ponters to index into memory.
.space genNextPtr 2
.space asave 1 ; place to store accumulator when needed.
.data BSS
.org $0300 ; page 3 is used for uninitialized data.
.space buff_curr 768 ; allocate arrays to hold current and next gens
.space buff_next 768
.text
;
; Macros
;
.macro incw
inc _1
bne _over
inc _1+1
_over:
.macend
; adds a 16 bit word and a constant and puts the result in first argument.
.macro addwi
clc
lda _1
adc #<_2
sta _1
lda _1+1
adc #>_2
sta _1+1
.macend
; adds two 16 bit words and puts the result in the first argument
.macro addw
clc
lda _1
adc _2
sta _1
lda _1+1
adc _2+1
sta _1+1
.macend
; adds the accumulator to a byte sum.
.macro addToSum
clc
adc _1
sta _1
.macend
; branch of word is zero
.macro beqw
lda _1
ora _1+1
beq _2
.macend
; branch if word not equal zero
.macro bnew
lda _1
ora _1+1
bne _2
.macend
; loads a word from a constant.
.macro loadwi
lda #<_2
sta _1
lda #>_2
sta _1+1
.macend
; loads a word from another word.
.macro loadw
lda _2
sta _1
lda _2+1
sta _1+1
.macend
.macro currAt
`loadw genCurrPtr, genCurr
`addw genCurrPtr, _1
`addw genCurrPtr, _2
lda (genCurrPtr)
.macend
; Prints a line feed.
.macro printcr
lda #AscLF
jsr _putch
.macend
.org $8000
.outfile "life.rom"
.advance $8000
;
; Functions
;
main:
jsr printWelcome
`loadwi strPtr, glider
jsr parseCurr
jsr life
brk
_welcome:
.byte "Game of Life",AscLF,0
printWelcome:
`loadwi strPtr, _welcome
jsr cputs
rts
; Sets the row offset to zero
rowFirst:
lda #$00
sta row
sta row+1
rts
; Advances the row offset by the width.
rowNext:
`addwi row, width
rts
; At end if current offset exceeds array size.
rowAtEnd?:
`loadw retval, row
`addwi retval, negSize
lda retval
ora retval+1
rts
; Iterator used to apply a function to the rows.
rowForEach:
.scope
jsr rowFirst
_loop:
jsr _indirectJmp
jsr rowNext
jsr rowAtEnd?
bne _loop
rts
_indirectJmp:
jmp (rowJmpPtr)
.scend
; Returns index of the row after current using wrap around.
rowPlus:
.scope
`loadw rowP, row ; calculate the next row
`addwi rowP, width
`addwi rowP, negSize ; check if zero?
`beqw rowP, _else
`loadw rowP, row ; otherwise redo calculation.
`addwi rowP, width
_else: rts ; on wrap around return zero
.scend
; Returns index of the column before current using wrap around.
rowMinus:
.scope
`beqw row, _else
`loadw rowM, row
`addwi rowM, negWidth
rts
_else:
`loadwi rowM, [size-width]
rts
.scend
colFirst:
lda #$00
sta col
sta col+1
rts
colNext:
`incw col
rts
colAtEnd?:
`loadw retval, col
`addwi retval, negWidth
lda retval
ora retval+1
rts
; Iterator used to apply a function to the cols.
colForEach:
.scope
jsr colFirst
_loop:
jsr _indirectJmp
jsr colNext
jsr colAtEnd?
bne _loop
rts
_indirectJmp:
jmp (colJmpPtr)
.scend
; Returns index of the column after current using wrap around.
colPlus:
.scope
`loadw colP, col ; calculate the next row
`incw colP
`addwi colP, negWidth ; check if zero?
`beqw colP, _else
`loadw colP, col ; otherwise redo calculation.
`incw colP
_else: rts ; on wrap around return zero
.scend
; Returns index of the column before current using wrap around.
colMinus:
.scope
`beqw col, _else
`loadw colM, col
`addwi colM, negOne
rts
_else:
`loadwi colM, [width-1]
rts
.scend
; Swaps current and next pointer.
moveCurr:
`loadw temp, genCurr
`loadw genCurr, genNext
`loadw genNext, temp
rts
; clears curr array to clear out junk in ram
eraseCurr:
.scope
`loadw genCurrPtr, genCurr
`loadwi temp, size
_loop:
lda #$00
sta (genCurrPtr)
`incw genCurrPtr
`addwi temp, negOne
`bnew temp, _loop
rts
.scend
; retrieve a cell value from the current generation
curr@:
`currAt row, col
rts
; stores a value into a cell from the current generation
curr!:
sta asave
`loadw genCurrPtr, genCurr
`addw genCurrPtr, row
`addw genCurrPtr, col
lda asave
sta (genCurrPtr)
rts
; stores a value into a cell from the next generation
next!:
sta asave
`loadw genNextPtr, genNext
`addw genNextPtr, row
`addw genNextPtr, col
lda asave
sta (genNextPtr)
rts
; Parses a pattern string referenced by strPtr into current board.
; This function is unsafe and will over write memory.
parseCurr:
.scope
; initialize the buffer pointers.
`loadwi genCurr, buff_curr
`loadwi genNext, buff_next
jsr eraseCurr
jsr rowFirst
jsr colFirst
_while: lda (strPtr)
beq _exit
clc
adc #$84 ; two's compliment of 7C |
beq _else
lda (strPtr)
clc
adc #$D6 ; two's compliment of 2A *
bne +
lda #$01
jsr curr!
* jsr colNext
jmp _next
_else:
jsr rowNext
jsr colFirst
_next: `incw strPtr
jmp _while
_exit: rts
.scend
printCurrCell:
.scope
jsr curr@
beq _else
lda #'\*
jsr _putch
rts
_else:
lda #'\.
jsr _putch
rts
.scend
; prints the row from the current generation to output
printCurrRow:
`printcr
jsr colForEach
rts
; Prints the current board generation to standard output
printCurr:
`loadwi rowJmpPtr, printCurrRow
`loadwi colJmpPtr, printCurrCell
jsr rowForEach
`printcr
rts
; Computes the sum of the neigbors of the current cell.
; Note: without a stack this is run on function. Macros make it tolerable.
calcSum:
jsr colMinus ; calculate neighboring cell indecies.
jsr rowMinus
jsr colPlus
jsr rowPlus
; Sum cell values for all eight neighbors.
`currAt rowM, colM
sta asave
`currAt rowM, col
`addToSum asave
`currAt rowM, colP
`addToSum asave
`currAt row, colM
`addToSum asave
`currAt row, colP
`addToSum asave
`currAt rowP, colM
`addToSum asave
`currAt rowP, col
`addToSum asave
`currAt rowP, colP
`addToSum asave
rts
calcCell:
.scope
jsr calcSum
; Unless explicitly marked live, all cells die in the next generation.
; There are two rules we'll apply to mark a cell live.
jsr curr@
bne _live ; Is the current cell dead?
lda asave
clc
adc #$fd ; A dead cell with 3 live neighbors
beq _markLive ; becomes a live cell.
bne _markDead
_live: ; Any live cell with two or three live neighbours survives.
lda asave
clc
adc #$fe
beq _markLive
lda asave
clc
adc #$fd
beq _markLive
_markDead: ; otherwise stay dead!
lda #$00
jsr next!
rts
_markLive:
lda #$01
jsr next!
rts ; else
.scend
calcRow:
jsr colForEach
rts
calcGen:
`loadwi rowJmpPtr, calcRow
`loadwi colJmpPtr, calcCell
jsr rowForEach
jsr moveCurr
rts
life:
.scope
_loop:
jsr printCurr
jsr calcGen
jmp _loop
.scend
; Test cases taken from Rosetta code's implementation
blinker:
.byte "|***",0
toad:
.byte "***| ***",0
pentomino:
.byte "**| **| *",0
pi:
.byte "**| **|**",0
glider:
.byte " *| *|***",0
pulsar:
.byte "*****|* *",0
ship:
.byte " ****|* *| *| *",0
pentadecathalon:
.byte "**********",0
clock:
.byte " *| **|**| *",0
; cputs is like the MSDOS console I/O function. It prints a null terminated
; string to the console using _putch.
cputs:
.scope
_loop: lda (strPtr) ; get the string via address from zero page
beq _exit ; if it is a zero, we quit and leave
jsr _putch ; if not, write one character
`incw strPtr ; get the next byte
jmp _loop
_exit: rts
.scend
; conio functions unique to each platform.
.alias _py65_putc $f001 ; Definitions for the py65mon emulator
.alias _py65_getc $f004
_getch:
.scope
* lda _py65_getc
beq -
rts
.scend
_putch:
.scope
sta _py65_putc
rts
.scend
; Interrupt handler for RESET button, also boot sequence.
; Note: This doesn't count for the restricted instruction challenge as
; these steps are required to set the hardware to a known state.
.scope
resetv:
sei ; diable interupts, until interupt vectors are set.
cld ; clear decimal mode
ldx #$FF ; reset stack pointer
txs
lda #$00 ; clear all three registers
tax
tay
pha ; clear all flags
plp
jmp main ; go to monitor or main program initialization.
.scend
; redirect the NMI interrupt vector here to be safe, but this
; should never be reached for py65mon.
irqv:
nmiv:
panic:
brk
; Interrupt vectors.
.advance $FFFA
.word nmiv ; NMI vector
.word resetv ; RESET vector
.word irqv ; IRQ vector