-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicboard.asm
253 lines (203 loc) · 6.4 KB
/
graphicboard.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
.enum 0
GraphicBoardButtonBit_Menu db
GraphicBoardButtonBit_Do db
GraphicBoardButtonBit_Pen db
.ende
ReadGraphicBoard:
; Main graphic board read
;
; If TL is 1, the graphic board has no pen data and only buttons are read.
; Bit 3 should always be 0.
; Bit 2 is 1 when the Pen button is down
; Bit 1 is 1 when the Do button is down
; Bit 0 is 1 when the Menu button is down
;
; Cycles |36|238|60|
; Read TL X
; TR ------|______|---
; TH -----------------
; Read data X
; Buttons^
;
; If it is 0, pen pressure is read.
; The data read is the high nibble followed by the low.
; If the value is too low, it stops there. (TODO: calculate the time - it is quite short.)
;
; Cycles |10|238|1266|533|45|533|???|
; Read TL X
; TR ------|_______________________|---
; TH ---------------|______|-----------
; Read data X X X
; Buttons^ Pressure
;
; If the pen pressure is OK, it continues on to read the X and Y positions.
;
; Cycles |10|238|1266|533|45|533|204|533|45|533|230|533|45|533|42|
; Read TL X
; TR ------|____________________________________________________|---
; TH ---------------|______|-------|______|-------|______|----------
; Read data X X X X X X X
; Buttons^ Pressure ^Pen X^^ ^Pen Y^^
;
; Note that during the title screen, the board is read with TH floating and TR high.
; In this state, the button data is returned. Thus it's unclear why this code waits
; before reading the buttons...
in a, (Port_IOPort1)
bit 4, a ; Check for data
jp nz, NoBoardData ; not pressed -> skip
; no delay: 10us from in to out
; ============================================================= Read 1 (TH = 1)
ld a, IO_TR1_Out0 | IO_TH1_Out1 | IO_TR2_Out0 | IO_TH2_Out0 ; $20 = all output, all zero except P1 TH = 1
out (Port_IOPortControl), a
ld b,16
-: djnz - ; delay: 238 cycles = 66us from out to in
; Read the buttons and calculate what's new since last time
ld a, (RAM_ButtonsPressed)
ld b, a
in a, (Port_IOPort1)
cpl
ld c, a
ld (RAM_ButtonsPressed), a
xor b
and c
ld (RAM_ButtonsNewlyPressed), a
ld b, 92
-: djnz - ; delay: 1266 cyles = 354us from in to out
nop
nop
; ============================================================= Read 2 (TH = 0, 1)
ld a, IO_TR1_Out0 | IO_TH1_Out0 | IO_TR2_Out0 | IO_TH2_Out0 ; all output, all zero
out (Port_IOPortControl), a
ld b, 40
-: djnz - ; delay: 533 cycles = 149us from out to in
; read low 4 bits
in a, (Port_IOPort1)
and %00001111
rlca
rlca
rlca
rlca
ld d, a
; no delay: 45 cycles = 13us from in to out
ld a, IO_TR1_Out0 | IO_TH1_Out1 | IO_TR2_Out0 | IO_TH2_Out0
out (Port_IOPortControl), a
ld b, 40
-: djnz - ; delay: 533 cycles = 149us from out to in
in a, (Port_IOPort1)
and %00001111
or d
cp 253
jp c, PressureTooLow
ld (RAM_Pressure), a
ld b, 11
-: djnz - ; delay: 204 cycles = 57 cycles from in to out
; ============================================================= Read 3 (TH = 0, 1)
ld a, IO_TR1_Out0 | IO_TH1_Out0 | IO_TR2_Out0 | IO_TH2_Out0
out (Port_IOPortControl), a
ld b, 40
-: djnz - ; delay: 533 cycles = 149us from out to in
in a, (Port_IOPort1)
and %00001111
rlca
rlca
rlca
rlca
ld h, a
; no delay: 45 cycles = 13us from in to out
ld a, IO_TR1_Out0 | IO_TH1_Out1 | IO_TR2_Out0 | IO_TH2_Out0
out (Port_IOPortControl), a
ld b, 40
-: djnz - ; delay: 533 cycles = 149us from out to in
in a, (Port_IOPort1)
and %00001111
or h
ld (RAM_PenX), a
nop
ld b, 14
-: djnz - ; delay: 230 cycles = 64us from in to out
; ============================================================= Read 4 (TH = 0, 1)
ld a, IO_TR1_Out0 | IO_TH1_Out0 | IO_TR2_Out0 | IO_TH2_Out0
out (Port_IOPortControl), a
ld b, 40
-: djnz - ; delay: 533 cycles = 149us from out to in
in a, (Port_IOPort1)
and %00001111
rlca
rlca
rlca
rlca
ld d, a
; no delay: 45 cycles = 13us from in to out
ld a, IO_TR1_Out0 | IO_TH1_Out1 | IO_TR2_Out0 | IO_TH2_Out0
out (Port_IOPortControl), a
ld b, 40
-: djnz - ; delay: 533 cycles = 149us from out to in
in a, (Port_IOPort1)
and %00001111
or d
ld (RAM_PenY), a
; ============================================================= Done: set TR, TH
; no delay: 42 cycles = 12us from in to out
ld a, IO_TR1_Out1 | IO_TH1_Out1 | IO_TR2_Out0 | IO_TH2_Out0 ; $30
out (Port_IOPortControl), a
ld a, (RAM_PenX)
ld c, a
ld a, (RAM_Pen_Smoothed.x)
or a
jp z, +
; RAM_Pen_Smoothed.x = (RAM_Pen_Smoothed.x + RAM_PenX) / 2
ld b, 0
ld h, 0
ld l, a
add hl, bc ; add together
ld e, 2
call DivMod_hl_e_a_e ; very slow way to do this
ld (RAM_Pen_Smoothed.x), a
jp ++
+: ld a, c
ld (RAM_Pen_Smoothed.x), a
++: ld a, (RAM_PenY)
ld c, a
ld a, (RAM_Pen_Smoothed.y)
or a
jp z, +
; RAM_Pen_Smoothed.y = (RAM_Pen_Smoothed.y + RAM_PenY) / 2
ld b, 0
ld h, 0
ld l, a
add hl, bc
ld e, 2
call DivMod_hl_e_a_e ; very slow way to do this
ld (RAM_Pen_Smoothed.y), a
ret
+: ld a, c
ld (RAM_Pen_Smoothed.y), a
ret
PressureTooLow:
ld a, IO_TR1_Out1 | IO_TH1_Out1 | IO_TR2_Out0 | IO_TH2_Out0
out (Port_IOPortControl), a
ret
; Board doesn't want to give us data
; Note that this is rather duplicated from above, it looks like it
; could be optimised a bit
NoBoardData:
; no delay: 36 cycles = 10us from in to out
; Set TH
ld a, IO_TR1_Out0 | IO_TH1_Out1 | IO_TR2_Out0 | IO_TH2_Out0
out (Port_IOPortControl), a
ld b, 16
-: djnz - ; delay: 238 cycles = 66us from out to in
; Read the buttons and calculate what's new since last time
ld a, (RAM_ButtonsPressed)
ld b, a
in a, (Port_IOPort1)
cpl
ld c, a
ld (RAM_ButtonsPressed), a
xor b
and c
ld (RAM_ButtonsNewlyPressed), a
; no delay: 60 cycles = 17us from in to out
ld a, IO_TR1_Out1 | IO_TH1_Out1 | IO_TR2_Out0 | IO_TH2_Out0
out (Port_IOPortControl), a
ret