-
Notifications
You must be signed in to change notification settings - Fork 2
/
grid.asm
619 lines (500 loc) · 10.2 KB
/
grid.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
; Super Sudoku - A Sudoku game for the SNES
; Copyright 2019-2022 Raphaël Assenat <raph@raphnet.net>
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
; OTHER DEALINGS IN THE SOFTWARE.
.include "header.inc"
.include "snesregs.inc"
.include "misc_macros.inc"
.include "zeropage.inc"
.include "puzzles.inc"
.include "grid.inc"
.bank 0 slot 1
.ramsection "grid_variables" SLOT RAM_SLOT
; griddata: dsw 81
gridarg_value: db
gridarg_padding: db
gridarg_x: dw
gridarg_y: dw
grid_vram_ptr: dw
gridsync_row_count: dw
.ends
.16BIT
.section "Grid LUT" FREE
.include "neighbors.asm"
; The above defines neighbor_list, which is a 9x9 array of words. Each word a pointer to a list of neighbors.
; Neighbor lists are 21 words each.
; Each word is the offset for the cell within gridata.
; The 21th is zero and used for stopping
.ends
.section "Grid" FREE
; now in grid.inc
;.define INITIAL_DIGIT_PAL ($20 | ($4<<2))
;.define ADDED_DIGIT_PAL ($20 | ($5<<2))
;.define HINTED_DIGIT_PAL ($20 | ($6<<2))
;.define BRUTEFORCED_DIGIT_PAL ($20 | ($7<<2))
.define GRID_BGMAP_PITCH 32
.define GRID_UPPER_LEFT_Y 6
.define GRID_UPPER_LEFT_X 2
.define GRID_ZERO_CHAR $30 ; Which tile is 0
.define GRID_BG_VRAM_ADDRESS $3000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Update the screen (VRAM) according to griddata
;
; Input: griddata
;
; Uses: grid_vram_ptr, gridsync_row_count
;
grid_syncToScreen:
pushall
A16
XY16
lda #0
clc
adc #GRID_UPPER_LEFT_Y * GRID_BGMAP_PITCH
adc #GRID_UPPER_LEFT_X ; Advance to X
adc #GRID_BG_VRAM_ADDRESS
sta grid_vram_ptr
; Counter for rows
ldy #9
sty gridsync_row_count
ldy #0 ; Index in griddata
; Set VRAM pointer to upper/left cell
lda grid_vram_ptr
sta VMADDL
bra @first_row ; Skip the increment
@row_loop:
lda grid_vram_ptr ; fetch previous row value
clc
adc #64
sta grid_vram_ptr ; remember new start cell
sta VMADDL ; Set vram destination
@first_row:
; Prepare counter for 9 cells
ldx #9
@col_loop:
lda griddata, Y
iny ; Next cell in griddata
iny
sta VMDATAL ; Value
stz VMDATAL ; Skip grid
dex
bne @col_loop
dec gridsync_row_count
bne @row_loop
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if the specified value (gridarg_value) is found in the
; neighbors of a given cell (gridarg_x, gridarg_Y)
;
; Input: gridarg_value, gridarg_x, gridarg_Y
;
; Output: TBD
;
grid_checkNeighborsForValue:
pha
phx
phy
php
A8
; make sure this is 0 (allows 16 bit read later)
stz gridarg_padding
A16
XY16
; Multiply by 18 (grid pitch)
lda gridarg_y
asl ; x2
asl ; x4
asl ; x8
asl ; x16
adc gridarg_y ; 18x = 16x + x + x
adc gridarg_y
; Add X (2x)
adc gridarg_x
adc gridarg_x
; A now contains the offset into griddata or neighbor_list. Move
; to Y to use as index
tay
; Get the pointer to the list of neighbors for the designated cell
lda neighbor_list, Y
;
sta dp_indirect_tmp1
ldy #0
@checknext:
lda (dp_indirect_tmp1),Y ; Load offset for neighbor
tax ; Move the offset to X to use it
lda griddata, X ; Load the value at this position
and #$FF
cmp gridarg_value ; Check if it is the value we are looking for
beq @foundit ; Yes? Godd!
iny ; Advance to next neighbor in list
iny
cpy #20*2
bne @checknext
; All neighbors checked, no match found
@finished:
plp
clc
bra @pp
@foundit:
; TODO ? Perhaps remember *where* we found it to show the user why he can't place it here?
plp
sec ; Exit with carry set
@pp:
ply
plx
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Let X equal the griddata offset for coordinate
;
; Input: gridarg_x, gridarg_Y
; Output: X
;
; Note: Call with 16-Bit X!
;
grid_getDataOffset:
pha
A16
; Multiply by 18 (grid pitch)
lda gridarg_y
asl ; x2
asl ; x4
asl ; x8
asl ; x16
adc gridarg_y ; 18x = 16x + x + x
adc gridarg_y
; Add X (2x)
adc gridarg_x
adc gridarg_x
; A now contains the offset into griddata. Move
; to Y to use as index
tax
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if a given grid cell is EMPTY
;
; Input: gridarg_value, gridarg_x, gridarg_Y
; Output: Carry flag (when set: Not empty)
;
grid_isEmptyAt:
pha
phx
phy
php
A16
XY16
; Multiply by 18 (grid pitch)
lda gridarg_y
asl ; x2
asl ; x4
asl ; x8
asl ; x16
adc gridarg_y ; 18x = 16x + x + x
adc gridarg_y
; Add X (2x)
adc gridarg_x
adc gridarg_x
; A now contains the offset into griddata. Move
; to Y to use as index
tay
A8
; If 0, cell empty.
lda griddata, Y
beq @empty
; non-zero : Not empty. Set carry and return
plp
sec
bra @pp
@empty:
; empty. Clear carry and return
plp
clc
@pp:
ply
plx
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if the grid is solved.
;
; Solved = number in all cells. No checks for errors required since
; the game prevents invalid entries already.
;
; Input: griddata
; Output: Carry flag. (Set = not solved)
;
grid_checkIfSolved:
pushall
A16
XY16
ldx #0
@lp:
lda griddata, X
and #$ff ; keep only number (ignore attributes)
beq @ret_sec ; If zero, grid is not full and therefore not solved.
inx
inx
cpx #162
bne @lp
; No zeros found = grid full = solved!
@ret_clc:
popall
clc
rts
@ret_sec:
popall
sec
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if a user value can be inserted in the designated cell.
;
; Rules:
; - Default digits that are part of the puzzle cannot be replaced/deleted
; - Inserting a zero is a deletion operation
; - Sudoku rules
;
; Input: gridarg_value, gridarg_x, gridarg_Y
; Output: Carry flag (when set: Refuse move)
;
grid_canInsertValueAt:
pha
phx
phy
php
; If the number to insert is a zero, skip looking for other zeros
; in neighbor cells.
A8
lda gridarg_value
beq @its_zero
; First check if the number in argument can be added to this cell
; based on neighbor rules
jsr grid_checkNeighborsForValue
bcs @refuse
@its_zero:
A16
XY16
; Multiply by 18 (grid pitch)
lda gridarg_y
asl ; x2
asl ; x4
asl ; x8
asl ; x16
adc gridarg_y ; 18x = 16x + x + x
adc gridarg_y
; Add X (2x)
adc gridarg_x
adc gridarg_x
; A now contains the offset into griddata. Move
; to Y to use as index
tay
A8
; First look at the number. If 0, allow replacment.
lda griddata, Y
beq @replace_ok
; Now look at the palette/priority byte. If non-equal to initial
; digit properties, accept.
iny
lda griddata, Y
cmp #INITIAL_DIGIT_PAL
bne @replace_ok
; Set carry to refuse
@refuse:
plp
sec
bra @pp
@replace_ok:
plp
clc
@pp:
ply
plx
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Insert a value at the specified offset.
;
; Used by solver.asm
;
; Arguments:
;
; - X : Offset in grid_value
; - A : Value
;
; Destroys: A
;
AXY16
grid_insertHintedValueOffset:
ora #(HINTED_DIGIT_PAL<<8)
sta griddata, X
; lda #1 ; Use A. When clearing a cell the grid won't update, but it does not matter.
sta grid_changed
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Insert a value at the current cursor position
;
; Input: gridarg_value (B)
;
;
grid_insertHintedValueAt:
pha
phx
phy
php
A16
XY16
; Multiply by 18 (grid pitch)
lda gridarg_y
asl ; x2
asl ; x4
asl ; x8
asl ; x16
adc gridarg_y ; 18x = 16x + x + x
adc gridarg_y
; Add X (2x)
adc gridarg_x
adc gridarg_x
; A now contains the offset into griddata. Move
; to Y to use as index
tay
A8
lda gridarg_value
clc
sta griddata, Y
lda #HINTED_DIGIT_PAL
iny
sta griddata, Y
; Trigger a redraw
lda #1
sta grid_changed
plp
ply
plx
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Insert a value at the current cursor position
;
; Input: gridarg_value (B)
;
;
grid_insertValueAt:
pha
phx
phy
php
A16
XY16
; Multiply by 18 (grid pitch)
lda gridarg_y
asl ; x2
asl ; x4
asl ; x8
asl ; x16
adc gridarg_y ; 18x = 16x + x + x
adc gridarg_y
; Add X (2x)
adc gridarg_x
adc gridarg_x
; A now contains the offset into griddata. Move
; to Y to use as index
tay
A8
lda gridarg_value
clc
sta griddata, Y
lda #ADDED_DIGIT_PAL
iny
sta griddata, Y
; Trigger a redraw
lda #1
sta grid_changed
plp
ply
plx
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Remove the digits that were added by the bruteforce solver.
;
grid_removeBruteForced:
pushall
AXY16
ldx #0
@lp:
lda griddata, X
and #$ff00
cmp #BRUTEFORCED_DIGIT_PAL
bne @skip
stz griddata, X
@skip:
inx
inx
cpx #81*2
bne @lp
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Init the grid data to all empty cells.
;
grid_init_blank:
pushall
Memset griddata 0 81*2
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Init the grid data array by copying from puzzle_buffer
;
grid_init_puzzle:
pushall
XY16
A8
ldy #0
ldx #0
@next_byte:
lda puzzle_buffer, X ; Read puzzle byte
sta griddata, Y
iny
lda #INITIAL_DIGIT_PAL.B
sta griddata, Y
iny
inx
cpy #81*2 ; _sizeof_griddata
bne @next_byte
@done:
; jsr grid_syncToScreen
; Trigger a redraw
lda #1
sta grid_changed
popall
rts
.ends