summaryrefslogtreecommitdiff
path: root/life.asm
blob: 1e3a6af0e270e5d8545930b3c2f90aa26025902a (plain)
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
.export init
.feature c_comments

numL := $02 ; low byte of a 16-bit number
numH := $03 ; low byte of a 16-bit number
key_buf_len := $c6
key_buf := $0277

; initialisation: clear screen and set num to point to screen mem start
init:
  jsr clear_screen
  ldy #$00
  lda #$04
  sta numH
  lda #$00
  sta numL
  jmp loop

; main loop
loop:
  jsr key_wait
  lda key_buf

  cmp #$1d ; right arrow
  beq if_right
  cmp #$9d ; left arrow
  beq if_left
  cmp #$2d ; reset if - pressed
  beq if_reset
  cmp #$20 ; fill cell if space pressed
  beq if_space
  cmp #$30 ; end program if 0 pressed
  bne loop
  jmp end

if_right:
  jsr inc_num
  jmp loop

if_left:
  jsr dec_num
  jmp loop

if_reset:
  lda #$04
  sta numH
  lda #$00
  sta numL
  jmp loop

if_space:
  ldx #$00
  lda (numL,x)
  cmp #$a0
  beq if_cell_filled
  jmp if_cell_empty
if_cell_filled:
  lda #$20
  jsr print_byte
  jmp loop
if_cell_empty:
  lda #$a0
  jsr print_byte
  jmp loop


; loops until keyboard buffer has any number of characters in it
key_wait:
  pha
key_wait_loop:
  lda key_buf_len ; shows how many keys are in key buffer
  cmp #$00
  beq key_wait_loop

  lda #$00
  sta key_buf_len
  pla
  rts

; clear the screen by filling it with spaces
; destroys: A, Y, numL & numH (in memory)
clear_screen:
  ; init: y = 0 because you can't use indirect addressing without a register,
  ; load start of screen memory into numL/numH
  ldy #0
  lda #$04
  sta numH
  lda #$00
  sta numL
  jmp cs_loop

  ; add 1 to high bit, return if $08 (screen mem only goes to $07ff)
  add_carry:
    inc numH
    lda numH
    cmp #$08
    bne cs_loop
    rts

  ; store a space in current screen mem address as specified by num, branch if
  ; number overflows to 0 (i.e. add 1 to high bit of 16 bit number)
  cs_loop:
    lda #$20
    sta (numL),y
    inc numL
    beq add_carry
    jmp cs_loop

; print byte in A at location in num
print_byte:
  ; init: store original A in X and push Y to stack
  tax
  tya
  pha
  txa

  ldy #$00
  sta (numL),y

  ; deinit: pull Y from stack and transfer original A from X to A
  pla
  tay
  txa
  rts

; adds A to num (16 bit) (NOT WORKING)
; destroys: X
a16b:
  pha
  tsx
  lda numL
  adc $0100,x
  sta numL
  bcs a16b_carry
  jmp a16b_end
  a16b_carry:
    inc numH
  a16b_end:
    clc
    pla
    rts

inc_num:
  inc numL
  beq inc_H
  rts
inc_H:
  inc numH
  rts

dec_num:
  pha

  dec numL
  lda numL
  cmp #$ff
  beq dec_H
  pla
  rts

dec_H:
  dec numH
  pla
  rts

end:
  brk