-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.s
456 lines (356 loc) · 8.48 KB
/
sudoku.s
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
;
; From: https://see.stanford.edu/materials/icspacs106b/H19-RecBacktrackExamples.pdf
;
; A straightforward port from C to 6502 assembler (although feeling like Forth!)
;
; Clock cycles:
;
; 65sc02 41,668,796 20.8 seconds @ 2Mhz
; 6502 46,013,289 23.0 seconds @ 2Mhz
.setcpu "6502"
.segment "OS"
start:
jmp reset
; http://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html
puzzle:
.byte 8, 0, 0, 0, 0, 0, 0, 0, 0
.byte 0, 0, 3, 6, 0, 0, 0, 0, 0
.byte 0, 7, 0, 0, 9, 0, 2, 0, 0
.byte 0, 5, 0, 0, 0, 7, 0, 0, 0
.byte 0, 0, 0, 0, 4, 5, 7, 0, 0
.byte 0, 0, 0, 1, 0, 0, 0, 3, 0
.byte 0, 0, 1, 0, 0, 0, 0, 6, 8
.byte 0, 0, 8, 5, 0, 0, 0, 1, 0
.byte 0, 9, 0, 0, 0, 0, 4, 0, 0
; Scratchpad area. Not sure how long this will be. Aliased within "proc" scope.
scratch := $80
; Some useful constants
UNASSIGNED = 0
BOARD_SIZE = 9
CELL_COUNT = (BOARD_SIZE * BOARD_SIZE)
.include "stack.inc"
.include "io.inc"
.proc print_board_element
lda #' '
jsr io::outchr
popx
lda puzzle,x
beq unassigned
adc #'0'
jsr io::outchr
jmp finish
unassigned:
lda #'-'
jsr io::outchr
finish:
lda #' '
jsr io::outchr
rts
.endproc
.proc print_box_break_vertical
lda #'|'
jsr io::outchr
rts
.endproc
.proc print_box_break_horizontal
jsr io::outstr
.asciiz " --------+---------+--------"
rts
.endproc
.proc print_newline
CR = $d
LF = $a
lda #CR
jsr io::outchr
lda #LF
jsr io::outchr
rts
.endproc
.proc print_board
jsr print_newline
jsr print_newline
jsr print_box_break_horizontal
jsr print_newline
ldy #0
loop:
pushy
jsr print_board_element
iny
; horizontal box break
lda table_move2box_y,y
bne boxh_continue
lda table_move2x,y
bne boxh_continue
jsr print_newline
jsr print_box_break_horizontal
boxh_continue:
; newline only
lda table_move2x,y
bne newl_continue
jsr print_newline
jmp continue
newl_continue:
; vertical box break
lda table_move2box_x,y
bne boxv_continue
jsr print_box_break_vertical
jmp continue
boxv_continue:
continue:
cpy #CELL_COUNT
bne loop
rts
.endproc
;
; ** Move and grid position translation methods
;
table_move2x:
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8
table_move2y:
.byte 0, 0, 0, 0, 0, 0, 0, 0, 0
.byte 1, 1, 1, 1, 1, 1, 1, 1, 1
.byte 2, 2, 2, 2, 2, 2, 2, 2, 2
.byte 3, 3, 3, 3, 3, 3, 3, 3, 3
.byte 4, 4, 4, 4, 4, 4, 4, 4, 4
.byte 5, 5, 5, 5, 5, 5, 5, 5, 5
.byte 6, 6, 6, 6, 6, 6, 6, 6, 6
.byte 7, 7, 7, 7, 7, 7, 7, 7, 7
.byte 8, 8, 8, 8, 8, 8, 8, 8, 8
table_move2box_x:
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
table_move2box_y:
.byte 0, 0, 0, 0, 0, 0, 0, 0, 0
.byte 1, 1, 1, 1, 1, 1, 1, 1, 1
.byte 2, 2, 2, 2, 2, 2, 2, 2, 2
.byte 0, 0, 0, 0, 0, 0, 0, 0, 0
.byte 1, 1, 1, 1, 1, 1, 1, 1, 1
.byte 2, 2, 2, 2, 2, 2, 2, 2, 2
.byte 0, 0, 0, 0, 0, 0, 0, 0, 0
.byte 1, 1, 1, 1, 1, 1, 1, 1, 1
.byte 2, 2, 2, 2, 2, 2, 2, 2, 2
; ** Row, column and box start positions
table_move2row_start:
.byte 0, 0, 0, 0, 0, 0, 0, 0, 0
.byte 9, 9, 9, 9, 9, 9, 9, 9, 9
.byte 18, 18, 18, 18, 18, 18, 18, 18, 18
.byte 27, 27, 27, 27, 27, 27, 27, 27, 27
.byte 36, 36, 36, 36, 36, 36, 36, 36, 36
.byte 45, 45, 45, 45, 45, 45, 45, 45, 45
.byte 54, 54, 54, 54, 54, 54, 54, 54, 54
.byte 63, 63, 63, 63, 63, 63, 63, 63, 63
.byte 72, 72, 72, 72, 72, 72, 72, 72, 72
table_move2box_start:
.byte 0, 0, 0, 3, 3, 3, 6, 6, 6
.byte 0, 0, 0, 3, 3, 3, 6, 6, 6
.byte 0, 0, 0, 3, 3, 3, 6, 6, 6
.byte 27, 27, 27, 30, 30, 30, 33, 33, 33
.byte 27, 27, 27, 30, 30, 30, 33, 33, 33
.byte 27, 27, 27, 30, 30, 30, 33, 33, 33
.byte 54, 54, 54, 57, 57, 57, 60, 60, 60
.byte 54, 54, 54, 57, 57, 57, 60, 60, 60
.byte 54, 54, 54, 57, 57, 57, 60, 60, 60
; Function: is_used_in_row
; ------------------------
; Returns a boolean which indicates whether any assigned entry
; in the specified row matches the given number.
.macro is_used_in_row ; ( a:number x:n -- zero:used non-zero:unused )
ldy table_move2row_start,x
cmp puzzle,y
beq row_used
cmp puzzle+1,y
beq row_used
cmp puzzle+2,y
beq row_used
cmp puzzle+3,y
beq row_used
cmp puzzle+4,y
beq row_used
cmp puzzle+5,y
beq row_used
cmp puzzle+6,y
beq row_used
cmp puzzle+7,y
beq row_used
cmp puzzle+8,y
row_used:
.endmacro
; Function: is_used_in_column
; ---------------------------
; Returns a boolean which indicates whether any assigned entry
; in the specified column matches the given number.
.macro is_used_in_column ; ( a:number x:n -- zero:used non-zero:unused )
ldy table_move2x,x
cmp puzzle,y
beq column_used
cmp puzzle+(BOARD_SIZE*1),y
beq column_used
cmp puzzle+(BOARD_SIZE*2),y
beq column_used
cmp puzzle+(BOARD_SIZE*3),y
beq column_used
cmp puzzle+(BOARD_SIZE*4),y
beq column_used
cmp puzzle+(BOARD_SIZE*5),y
beq column_used
cmp puzzle+(BOARD_SIZE*6),y
beq column_used
cmp puzzle+(BOARD_SIZE*7),y
beq column_used
cmp puzzle+(BOARD_SIZE*8),y
column_used:
.endmacro
; Function: is_used_in_box
; ------------------------
; Returns a boolean which indicates whether any assigned entry
; within the specified 3x3 box matches the given number.
.macro is_used_in_box ; ( a:number x:n -- zero:used non-zero:unused )
ldy table_move2box_start,x
cmp puzzle,y
beq box_used
cmp puzzle+1,y
beq box_used
cmp puzzle+2,y
beq box_used
cmp puzzle+BOARD_SIZE,y
beq box_used
cmp puzzle+BOARD_SIZE+1,y
beq box_used
cmp puzzle+BOARD_SIZE+2,y
beq box_used
cmp puzzle+(BOARD_SIZE*2),y
beq box_used
cmp puzzle+(BOARD_SIZE*2)+1,y
beq box_used
cmp puzzle+(BOARD_SIZE*2)+2,y
box_used:
.endmacro
; Function: is_available
; ----------------------
; Returns a boolean which indicates whether it will be legal to assign
; number to the given row, column location.As assignment is legal if it that
; number is not already used in the row, column, or box.
.macro is_available ; ( a:number x:n -- zero:available non-zero:unavailable )
is_used_in_row
beq used
is_used_in_column
beq used
is_used_in_box
beq used
tay
lda #0
beq done
used:
tay
; y *must* be non-zero, clearing the zero flag implicitly
done:
.endmacro
; Function: solve
; ---------------
; Takes a partially filled - in grid and attempts to assign values to all
; unassigned locations in such a way to meet the requirements for sudoku
; solution(non - duplication across rows, columns, and boxes).The function
; operates via recursive backtracking : it finds an unassigned location with
; the grid and then considers all digits from 1 to "board-size" in a loop.If a digit
; is found that has no existing conflicts, tentatively assign it and recur
; to attempt to fill in rest of grid.If this was successful, the puzzle is
; solved.If not, unmake that decision and try again.If all digits have
; been examined and none worked out, return false to backtrack to previous
; decision point.
.proc solve ; ( n -- A:f )
phx
popx
cpx #CELL_COUNT
bne _not_finished
plx
lda #0
rts ; success!
_not_finished:
lda puzzle,x
beq _begin_loop
inx
pushx
plx
jmp solve ; if it's already assigned, skip
_begin_loop:
ldy #1
_loop:
tya
is_available
bne _loop_continue ; if looks promising
tya
sta puzzle,x ; make tentative assignment
pusha
inx
pushx
dex
jsr solve
pha
popy
pla
beq _return_true ; recur, if success, yay!
_loop_continue:
iny
cpy #BOARD_SIZE + 1 ; consider all digits
beq _round
jmp _loop
_round:
; failure, unmake & try again
.ifpsc02
stz puzzle,x
.else
lda #UNASSIGNED
sta puzzle,x
.endif
_return_false:
plx
; x *must* be non-zero
rts ; this triggers backtracking
_return_true:
plx
lda #0
rts
.endproc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
reset:
jsr stack::init
jsr io::outstr
.asciiz "Solving puzzle: "
lda #0
pusha
jsr solve
bne fail
jsr io::outstr
.asciiz "pass"
jsr print_board
jmp end
fail:
jsr io::outstr
.asciiz "fail"
end:
jsr io::outstr
.byte $a, 0
brk
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
nmi: jmp nmi
irq_brk:
jmp irq_brk ;
.segment "VECTORS"
.word nmi ; NMI
.word reset ; RESET
.word irq_brk ; IRQ/BRK