-
Notifications
You must be signed in to change notification settings - Fork 4
/
furryrpg_sram.inc.asm
130 lines (99 loc) · 3.06 KB
/
furryrpg_sram.inc.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
;==========================================================================================
;
; "FURRY RPG" (WORKING TITLE)
; (c) 2023 by Ramsis a.k.a. ManuLöwe (https://manuloewe.de/)
;
; *** SRAM HANDLER ***
;
;==========================================================================================
CheckSRAM:
.ACCU 8
.INDEX 16
lda #bankbyte(SRAM) ; set start address to SRAM data
sta <DP2.Temp+7
Accu16
lda #loword(SRAM)
sta <DP2.Temp+5
lda #$01FE ; assume ChecksumCmpl = $FFFF and Checksum = $0000, so add these up right now
sta <DP2.Temp+3
ldy #0
- lda [<DP2.Temp+5], y
and #$00FF
clc
adc <DP2.Temp+3
sta <DP2.Temp+3
iny
cpy #lobyte(SRAM.ChecksumCmpl) ; location of SRAM checksum complement & checksum reached? // self-reminder: Use low byte here as y is zero-based and Temp+5 contains SRAM offset in middle byte. As it's an immediate value, WLA DX will still generate $00 as the high byte of a 16-bit operand.
bne -
ldy #lobyte(SRAM.Checksum+2) ; skip both // ditto
- lda [<DP2.Temp+5], y
and #$00FF
clc
adc <DP2.Temp+3
sta <DP2.Temp+3
iny
cpy #8192 ; all SRAM data checked?
bne -
lda <DP2.Temp+3 ; Temp+3 now contains the sum of all SRAM bytes
cmp SRAM.Checksum ; compare sum to checksum
bne @ClearSRAM
eor #$FFFF ; compare checksum XOR $FFFF to complement
cmp SRAM.ChecksumCmpl
beq @SRAMGood
@ClearSRAM: ; checksum and/or complement invalid means SRAM is corrupt
lda #$0000
ldx #$0000
- sta SRAM, x ; zero out SRAM data
inx
inx
cpx #8192
bne -
lda #$01FE ; write good checksum (has to be $01FE when everything else is zero)
sta SRAM.Checksum
xba ; write checksum complement ($FE01)
sta SRAM.ChecksumCmpl
@SRAMGood:
Accu8
rtl
FixSRAMChecksum:
lda #bankbyte(SRAM) ; set start address to SRAM data
sta <DP2.Temp+7
Accu16
lda #loword(SRAM)
sta <DP2.Temp+5
lda #$01FE ; assume ChecksumCmpl = $FFFF and Checksum = $0000, so add these right now
sta <DP2.Temp+3
ldy #0
- lda [<DP2.Temp+5], y
and #$00FF
clc
adc <DP2.Temp+3
sta <DP2.Temp+3
iny
cpy #lobyte(SRAM.ChecksumCmpl) ; location of SRAM checksum complement & checksum reached? // self-reminder as in CheckSRAM above
bne -
ldy #lobyte(SRAM.Checksum+2) ; skip both // ditto
- lda [<DP2.Temp+5], y
and #$00FF
clc
adc <DP2.Temp+3
sta <DP2.Temp+3
iny
cpy #8192 ; end of SRAM data reached?
bne -
lda <DP2.Temp+3 ; write new checksum and complement
sta SRAM.Checksum
eor #$FFFF
sta SRAM.ChecksumCmpl
Accu8
rts
WriteDataToSRAM: ; this routine expects the 24-bit source data address in DP2.DataAddress, destination offset (with data length added) in X, and data length in Y
- dex ; counting down dest from (last data byte index + 1)
dey ; counting down src from transfer length
lda [<DP2.DataAddress], y
sta.l bankbyte(SRAM)<<16, x ; x contains offset in middle byte, so just use SRAM bank as a base for writing bytes
cpy #0 ; all bytes written?
bne -
jsr FixSRAMChecksum
rtl
; ******************************** EOF *********************************