summaryrefslogtreecommitdiffstats
path: root/hacks/images/m6502/crunch6502.asm
blob: 56ea5070a682c36396a8e8b699b249bc04db2278 (plain) (blame)
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
;; Show "6502" on the screen waving up and down.
;; Jeremy English 29-December-2007
;;
;; Each digit is stored as a pattern of vertical bits.
;; For example:
;;
;;   111111     This is the digit six. We think of the digit 
;;   111111     by it's column pattern. The column patterns 
;;   110000     are labeled at the bottom of the example. 
;;   110000     Pattern B is 1100110011. The basic algorithm 
;;   111111     is that we get the pattern, paint the first
;;   111111     bit (1 foreground, 0 background) then dec y 
;;   110011     and get the next bit.
;;   110011     
;;   111111     The pattern for each digit is:
;;   111111     6 = AABBCC
;;   ------     5 = DDBBCC
;;   AABBCC     0 = AAEEAA
;;              2 = CCBBDD

;; Addresses $0 and $1 are used by the paint subroutine.
;; Addresses $2 through $6 are used by the display pattern subroutine
;; Address $7 is used in the main loop
;; Address $8 through $1a  are used for the start positions
;; Address $1b is used by the display pattern subroutine
;; Address $1c is used as the color row offset.
;; Addresses $d0 through $ef store the font table

jmp init_font_table
start:

;; Initialize the pointers to the start position.
lda #<y_start_pos1
sta $b
lda #>y_start_pos1
sta $c
lda #<y_start_pos2
sta $d
lda #>y_start_pos2
sta $e
lda #<y_start_pos3
sta $f
lda #>y_start_pos3
sta $10
lda #<y_start_pos4
sta $11
lda #>y_start_pos4
sta $12
lda #<y_start_pos5
sta $13
lda #>y_start_pos5
sta $14
lda #<y_start_pos4
sta $15
lda #>y_start_pos4
sta $16
lda #<y_start_pos3
sta $17
lda #>y_start_pos3
sta $18
lda #<y_start_pos2
sta $19
lda #>y_start_pos2
sta $1a


lda #0        ; start position to use
sta $8

main_loop:
inc $1c       ; increment the color offset.
inc $1d       ; increment the starting x position
ldy $8        ; load the current start position index
ldx $b,y      ; get the lsb from the table
txa
sta $9        ; store the msb of the start position pointer
iny           ; move to the next position in the table
ldx $b,y      ; get the msb from the table
txa
sta $a        ; store the lsb of the start position pointer
iny           ; move the index up by one
tya
cmp #$10       ; have we looked at all 16 start positions?
bne store_idx ; if not then keep the index and store it
lda #0        ; set the index back to zero
store_idx:
sta $8        ; save the index back in memory

ldy #0
lda #$ff
sta $4        ; initialize the column to FF
display_loop:
  inc $4      ; increment the column
  ldx $d0,y   ; load the lsb from the font table
  stx $2
  iny
  ldx $d0,y   ; load the msb from the font table
  stx $3
  sty $7      ; save y in memory
  jsr dis_pat ; Jump to the display pattern subroutine.
  inc $4      ; increment the column	
  jsr dis_pat ; Each pattern gets painted twice so we have a thicker font
  ldy $7      ; get y out of memory
  iny         ; increment the index
  tya
  cmp #$20    ; Did we display all of the columns?
  bne display_loop ;if not continue
jmp main_loop
rts

init_font_table:
  ;;Setup a table in the zero page that contains the string "6502"
  lda #<pattern_a    ;start with digit 6. It's pattern is aabbcc
  sta $d0
  lda #>pattern_a
  sta $d1
  lda #<pattern_b
  sta $d2
  lda #>pattern_b
  sta $d3
  lda #<pattern_c
  sta $d4
  lda #>pattern_c
  sta $d5
  lda #<pattern_null  ;We want to space everything out with blanks
  sta $d6
  lda #>pattern_null
  sta $d7
  lda #<pattern_d   ;load memory for digit 5 ddbbcc
  sta $d8
  lda #>pattern_d
  sta $d9
  lda #<pattern_b
  sta $da
  lda #>pattern_b
  sta $db
  lda #<pattern_c
  sta $dc
  lda #>pattern_c
  sta $dd
  lda #<pattern_null
  sta $de
  lda #>pattern_null
  sta $df
  lda #<pattern_a   ;load memory for digit 0 aaeeaa
  sta $e0
  lda #>pattern_a
  sta $e1
  lda #<pattern_e
  sta $e2
  lda #>pattern_e
  sta $e3
  lda #<pattern_a
  sta $e4
  lda #>pattern_a
  sta $e5
  lda #<pattern_null
  sta $e6
  lda #>pattern_null
  sta $e7
  lda #<pattern_c   ;load memory for digit 2 ccbbdd
  sta $e8
  lda #>pattern_c
  sta $e9
  lda #<pattern_b
  sta $ea
  lda #>pattern_b
  sta $eb
  lda #<pattern_d
  sta $ec
  lda #>pattern_d
  sta $ed
  lda #<pattern_null
  sta $ee
  lda #>pattern_null
  sta $ef
  jmp start


;; Display a pattern on the screen. The pattern to use is 
;; stored at $2 and $3. The current column is stored at $4.
dis_pat:
  ldy $4             ; Load the current column into y
  lda ($9),y         ; Get the start position for y
  tay
  sty $5             ; Store the starting position in memory
  ldy #0             ; We have 12 bits that need to be painted
dis_pat_loop:
  lda ($2),y         ; get a bit from the pattern
  pha                ; save the color on the stack
  tya                ; move the index into the accumulator
  clc                ; clear the carry 
  adc $5             ; add the starting position to the index
  sty $6             ; store the index 
  tay                ; The calculated y position
  ldx $4             ; The x position is the current column
  pla                ; pop the color off of the stack
  beq go_paint       ; black just paint it
  clc                ; get rid of any carry bit
  sty $1b            ; save the y coordinate
  tya
  clc
  adc $1c            ; add the color offset
  and #$7            ; make sure the look up is in range
  tay                ; move the new index into y so we can look up the color
  lda color_row,y    ; if not black get the row color
  ldy $1b            ; restore the y coordinate
go_paint:
  jsr paint          ; paint the pixel on the screen
  ldy $6             ; get the index out of memory
  iny                ; increment the index
  tya
  cmp #12            ; Have we looked at all of the bits?
  bne dis_pat_loop   ; if not then continue looking
  rts                ; else return from the subroutine

;; Paint - Put a pixel on the screen by using the x registry for 
;;         the x position, the y registry for the y position and 
;;         the accumulator for the color.
paint:
   pha           ; Save the color
   lda yl,y      ; Get the LSB of the memory address for y
   sta $0        ; Store it first
   lda yh,y      ; Get the MSB of the memory address for y
   sta $1        ; Store it next
   txa           ; We want x in the y registry so we transfer it to A
   tay           ; and then A into y.
   pla           ; Pop the color off of the stack
   sta ($0),y    ; Store the color at the correct y + x address.
   rts           ; return from the subroutine.

;; Paint uses the following two tables to look up the 
;; correct address for a y coordinate between 
;; 0 and 31.

;; Y cord MSB
yh:
       dcb $02, $02, $02, $02, $02, $02, $02, $02
       dcb $03, $03, $03, $03, $03, $03, $03, $03
       dcb $04, $04, $04, $04, $04, $04, $04, $04
       dcb $05, $05, $05, $05, $05, $05, $05, $05
;; Y cord LSB
yl:
       dcb $00, $20, $40, $60, $80, $a0, $c0, $e0
       dcb $00, $20, $40, $60, $80, $a0, $c0, $e0
       dcb $00, $20, $40, $60, $80, $a0, $c0, $e0
       dcb $00, $20, $40, $60, $80, $a0, $c0, $e0 

;; A zero is on the end of each pattern to clean up 
;; residue left by waving.
pattern_a:
  dcb 0,1,1,1,1,1,1,1,1,1,1,0

pattern_b:
  dcb 0,1,1,0,0,1,1,0,0,1,1,0

pattern_c:
  dcb 0,1,1,0,0,1,1,1,1,1,1,0

pattern_d:
  dcb 0,1,1,1,1,1,1,0,0,1,1,0

pattern_e:
  dcb 0,1,1,0,0,0,0,0,0,1,1,0

pattern_null:
  dcb 0,0,0,0,0,0,0,0,0,0,0,0

;; Table that store the current start position 
;; of each y column.
y_start_pos1:
  dcb 10,10,9,9,8,8,7,7,6,6,7,7,8,8,9,9,10,10,9,9,8,8,7,7
  dcb 6,6,7,7,8,8

y_start_pos2:
  dcb 9,9,8,8,8,8,8,8,7,7,8,8,8,8,8,8,9,9,8,8,8,8,8,8
  dcb 7,7,8,8,8,8

y_start_pos3:
  dcb 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  dcb 8,8,8,8,8,8

y_start_pos4:
  dcb 7,7,8,8,8,8,8,8,9,9,8,8,8,8,8,8,7,7,8,8,8,8,8,8
  dcb 9,9,8,8,8,8

y_start_pos5:
  dcb  6, 6,7,7,8,8,9,9,10,10,9,9,8,8,7,7, 6, 6,7,7,8,8,9,9
  dcb 10,10,9,9,8,8

color_row:
  dcb $7,$8,$9,$2,$4,$6,$e,$3,$d,$5