· 8 years ago · Jan 25, 2018, 12:50 AM
1;=============================================================================
2;Menu system and supporting routines
3;
4;
5
6
7;=============================================================================
8;Note: During initialization stage, any data that is set to -1 will cause that
9; value to be, instead, loaded from the indicated register
10;
11;Menu system data and routines formatting:
12;Initializer formatting | Internal memory structure
13;Ofs Reg Description |Ofs Description
14;+00 A Menu slot (0-7) |+00 Cursor X
15;+01 C Starting cursor X |+01 Cursor Y
16;+02 B Starting cursor Y |+02 dX
17;+03 E dX |+03 dY
18;+04 D dY |+04 X boundary
19;+05 L X boundary |+05 Y boundary
20;+06 H Y boundary |+06 Current X position
21;+07 IXl LSB Routine Table |+07 Current Y position
22;+08 IXh MSB Routine Table |+08 LSB Routine Table
23; |+09 MSB Routine Table
24;Bytes free at end : $8DxB-$8DxF
25;Routine Table (structure semi-variable)
26;
27;+0 Maskouts %Dl Md 2n y= Up Rg Lf Dn : Count # of bits =n. n=#of words
28;+1*2n Addresses from Dl to Dn in that order (left shift)
29;
30InitMenuSys:
31 ex (sp),hl ;free up HL for this time around and expose that address.
32 ex af,af' ;Swap out AF for now.
33 ld a,(hl)
34 inc hl
35 inc a
36 jr z,InitMenuSys1 ;manually load up pointer to menu data location
37 push hl
38 dec a
39 ld hl,menusysbuf
40 add a,a
41 add a,a
42 add a,a
43 add a,a
44 ld L,a
45 push hl
46 pop iy
47 pop hl
48 ex af,af'
49 jr InitMenuSys2 ;jump past initializer
50InitMenuSys1:
51 ex af,af'
52 push hl
53 ld hl,menusysbuf
54 add a,a
55 add a,a
56 add a,a
57 add a,a
58 ld L,a
59 push hl
60 pop iy
61 pop hl
62InitMenuSys2:
63 ld a,(myflags) ;it's where the sysflags location is
64 bit menurestore,a
65 jr nz,MenuRestorepoint ;IY=location in RAM thinger.
66 call InitMSysLoader ;C is already in the argument for this
67 ld c,b \ call INitMSysLoader ;B is next...
68 ld c,e \ call INitMSysLoader ;and so on according to the specs.
69 ld c,d \ call INitMSysLoader ;
70 ex (sp),hl
71 ex de,hl
72 ex (sp),hl
73 ld c,e \ call INitMSysLoader ;
74 ld c,d \ call INitMSysLoader ;
75 ld b,2
76InitMenuSys3:
77 ld (iy),0
78 inc iy
79 djnz InitMenuSys3
80 ld e,(hl)
81 inc hl
82 ld d,(hl)
83 inc hl
84 ld a,e
85 and d
86 inc a
87 jr nz,InitMenuSys4 ;if -1 (now zero), then load IX
88 push ix
89 pop de
90InitMenuSys4:
91 ld (iy+0),e
92 ld (iy+1),d ;stream loaded
93MenuRestorepoint:
94 push iy
95 pop hl
96 ld a,L
97 and %11110000
98 ld L,a
99 ld (curmenu),hl
100 ld d,(hl) ;cursorX
101 inc L
102 ld e,(hl) ;cursorY
103 ld (cursory),de
104 ld iy,myflags
105 loadslot2(RunMenuSys)
106 pop hl ;removing the calling routine to get to the base
107 set dotextmap,(iy+sysflags)
108 call savetextbuf
109 call PutCursor
110MenuDefault:
111 ret
112
113;MTSTDRV:
114;.db 1,1,2,2,3,3,0,0 \.dw TestAddresses
115InitMSysLoader:
116 ld a,(hl)
117 inc hl
118 inc a
119 jr nz,$+4
120 ld a,c
121 inc a
122 dec a
123 ld (iy),a
124 inc iy
125 ret
126
127
128RunMenuSys:
129 res menurestore,(iy+sysflags)
130; ld de,(cursory)
131; ld hl,(curmenu)
132; ld (hl),d ;cursorX
133; inc L
134; ld (hl),e ;cursorY
135 ld (itemp1),sp
136 ld sp,textshadow+384
137 ld b,48
138 ld hl,0
139 push hl
140 push hl
141 push hl
142 push hl
143 djnz $-4
144 ld sp,(itemp1)
145 ld a,(keyoutput)
146 cpl
147 ld L,a
148 ld bc,$0800
149 rlca
150 jr nc,$+3
151 inc c
152 djnz $-4
153 dec c
154 ret nz ;more than one key was pressed, or no keys. Do not allow continue.
155 ld ix,(curmenu)
156 ld e,(ix+8)
157 ld d,(ix+9)
158;=============================================================================
159;IF YOU WANT AN ON-HOVER EVENT, PLACE IT HERE. PRECEDE FLAGS CODE WITH
160;AN ADDRESS TO THE HOVER ROUTINE
161;
162;=============================================================================
163 ld a,(de)
164 inc de
165 ld c,L ;A=mask C=keycode
166 ld b,8
167 ld hl,MenuDefault
168RunMenuSysL1:
169 rlca ;get maskcode in A
170 jr nc,RunMenuSysS1 ;if not exist, skip trying and dummy out keycode move
171 rlc c ;if it exists, check keypress and see if there's an entry
172 jr nc,RunMenuSysS2 ;if not exist, skip load and dummy out table increment
173 ex de,hl
174 ld e,(hl)
175 inc hl
176 ld d,(hl)
177 inc hl
178 ex de,hl
179 jr RunMenuSysS3 ;jump to end of loop
180RunMenuSysS2:
181 inc de
182 inc de
183 jr RunMenuSysS3
184RunMenuSysS1:
185 rlc c ;even if it was a keypress, nothing corresponds to it.
186RunMenuSysS3:
187 djnz RunMenuSysL1 ;At end, C is the same. HL=address to jump to.
188 bit 5,c ;Special 2nd key support if it is set.
189 jr nz,RunMenuSys2nd
190 bit 6,c ;Special MODE key support : Menu restore.
191 jr z,$+6
192 set menurestore,(iy+sysflags)
193 ld a,c
194 and %11110000 ;checking for non-dpad lookups
195 jr z,$+3
196RunMenuSysJPHL:
197 jp (hl) ;goto routine found in HL if it's a non-dpad push
198 ld de,$0000
199 xor a
200 bit 0,c ;Down
201 jr z,RunMenuSysDS0
202 ld a,(ix+7)
203 cp (ix+5) ;if no carry, then boundary hit
204 jr nc,RunMenuSysJPHL
205 inc (ix+7)
206 ld e,(ix+3) ;changing cursor position...
207 jr RunMenuSysMove
208RunMenuSysDS0:
209 bit 1,c ;Left
210 jr z,RunMenuSysDS1
211 or (ix+6) ;A=0, so if (ix+6)=0, then it's all zero
212 jr z,RunMenuSysJPHL
213 dec (ix+6)
214 ld a,(ix+2)
215 neg
216 ld d,a
217 jr RunMenuSysMove
218RunMenuSysDS1:
219 bit 2,c ;Right
220 jr z,RunMenuSysDS2
221 ld a,(ix+6)
222 cp (ix+4) ;if no carry, then boundary hit
223 jr nc,RunMenuSysJPHL
224 inc (ix+6)
225 ld d,(ix+2) ;changing cursor position...
226 jr RunMenuSysMove
227RunMenuSysDS2: ;the final direction MUST be UP if it got to here
228 or (ix+7) ;A=0, so if (ix+6)=0, then it's all zero
229 jr z,RunMenuSysJPHL
230 dec (ix+7)
231 ld a,(ix+3)
232 neg
233 ld e,a
234 dec d ;fixing sign for DE as a whole.
235RunMenuSysMove: ;DE=change to be made in cursorY
236 push de
237 call LoadCursor
238 pop de
239 ld hl,(cursory)
240 add hl,de
241 ld (cursory),hl
242 ex de,hl
243 ld (ix+0),d
244 ld (ix+1),e
245 set dotextmap,(iy+sysflags)
246 jp PutCursor
247
248RunMenuSys2nd: ;set 7,h if address is table of addresses to follow. V-aligned
249;curY+(curX*(maxY+1))
250 xor a
251 ld b,(ix+5)
252 inc b
253RunMenuSys2ndMult:
254 add a,(ix+6)
255 djnz RunMenuSys2ndMult
256 add a,(ix+7)
257 bit 7,h
258 jr z,RunMenuSysJPHL
259 res 7,h
260 add a,a
261 call AddAtoHL0
262 ld a,(hl)
263 inc hl
264 ld h,(hl)
265 ld L,a
266 jp (hl)
267
268;Menu system data and routines formatting:
269;Initializer formatting | Internal memory structure
270;Ofs Reg Description |Ofs Description
271;+00 A Menu slot (0-7) |+00 Cursor X
272;+01 C Starting cursor X |+01 Cursor Y
273;+02 B Starting cursor Y |+02 dX
274;+03 E dX |+03 dY
275;+04 D dY |+04 X boundary
276;+05 L X boundary |+05 Y boundary
277;+06 H Y boundary |+06 Current X position
278;+07 IXl LSB Routine Table |+07 Current Y position
279;+08 IXh MSB Routine Table |+08 LSB Routine Table
280; |+09 MSB Routine Table
281
282LoadCursor:
283 ld hl,(cursory)
284 push hl
285 call GetTextPos ;HL=buffer address
286 ld e,L
287 ld d,H
288 ld bc,384
289 add hl,bc
290 ldi
291 inc L
292 inc E
293 ldi
294 ex de,hl
295 pop hl
296 inc h
297 call GetTextPos ;HL=buffer address
298 ld e,L
299 ld d,H
300 ld bc,384
301 add hl,bc
302 ldi
303 inc L
304 inc E
305 ldi
306 ret
307
308;=============================================================================
309;Routine used to create a dialog box.
310;Input: H=x1 L=y1 D=x2 E=y2 IX=strAdr0Term, A=HeaderHorizStart
311;(0,0) |--------------------------------|
312; | (x1,y1)------------------| |
313; | | Content goes here | |
314; | |------------------(x2,y2) |
315; |--------------------------------| (23,15)
316;Clears out the inside of the box with a light gray space
317;Must navigate the vertically-aligned pairs.
318
319BoxDraw:
320 neg
321 push af
322 ld a,d
323 sub h
324 sub 2 ;exclude two rows. Assumes the box is at least 2 high. Break.
325 ld b,a ;represents the number of columns to write.
326 ld a,e
327 sub l
328 sub 2 ;to exclude two columns. Assumes at least 2. Else, breaks.
329 ld c,a ;represents the number rows to write
330 pop af
331 add a,b
332 ld (itemp1),a ;saving this for later...
333 ex de,hl ;DE=x1,y1 used to start the routine.
334 ld hl,textshadow ;starting address of the textmap
335 ld a,d
336 ld d,0
337 add hl,de ;adding to correct position downward.
338 rra ;putting carry add in if this is uneven number.
339 adc hl,de ;because need to skip over *pairs* Carry is killed for next.
340 add a,a ;undo 1/2 division. Refers to even columns (0-22)
341 add a,a ;0-44
342 add a,a ;0-88
343 add a,a ;0-176. can't go higher than this.
344 ld e,a
345 add hl,de ;adding column
346 add hl,de ;0-352 max. HL now set to writing.
347 ld de,(16*2)-2 ;next column addition.
348BoxDraw1stCol:
349 push hl
350 ld (hl),$01
351 inc l \ inc l
352 ld a,c
353 ld (hl),$04
354 inc l \ inc l
355 dec c
356 jr nz,$-5
357 ld c,a
358 ld (hl),$06
359 pop hl
360 bit 0,l
361 inc hl
362 jr z,$+3
363 add hl,de
364BoxDrawCols:
365 push hl
366 ld a,(itemp1)
367 cp b ;activate - loopcount. on NC, write chars. Else, write border
368 jr c,BoxDrawColsDef
369 ld a,(ix)
370 or a
371 jr z,BoxDrawColsDef ;EOL encountered. Do not draw chars anymore.
372 cp $20
373 jr nz,$+4 ;if not a space character, omit adjustment for space.
374 add a,(-$17)+$33 ;adjustment prior to next adjustment.
375 add a,-$33 ;adjusting for A=$41 and charstart $0E
376 ld (hl),a
377 inc ix
378 jr $+4
379BoxDrawColsDef:
380 ld (hl),$02
381 inc l \ inc l
382 ld a,c
383 ld (hl),$09
384 inc l \ inc l
385 dec c
386 jr nz,$-5
387 ld c,a
388 ld (hl),$07
389 pop hl
390 bit 0,L
391 inc hl
392 jr z,$+3
393 add hl,de
394 djnz BoxDrawCols
395BoxDrawLastCol:
396 ld (hl),$03
397 inc l \ inc l
398 ld (hl),$05
399 inc l \ inc l
400 dec c
401 jr nz,$-5
402 ld (hl),$08
403 ret
404
405
406;=============================================================================
407;Box setup routines
408;
409;
410
411nullstring: .db 0
412SetupBoxes:
413 pop hl ;pointer to the data stream
414 ld b,(hl)
415 inc hl ;now points to the data stream.
416SetpuBoxesLoop:
417;Input: H=x1 L=y1 D=x2 E=y2 IX=address to display string, zero terminated
418;(0,0) A=Distance into box (neglect edges)
419 ld c,(hl) ;getting x1
420 inc hl
421 ld d,(hl) ;getting x2 to correct place
422 inc hl
423 ld e,(hl) ;temporary spot and halfway correct thing.
424 inc hl
425 bit 7,c
426 jr nz,SetupBoxesLong
427 ld a,2
428 ld ix,nullstring
429SetupBoxesCont:
430 push hl
431 push af
432 ld h,c
433 ld a,e ;incomplete. Still have to strip MSnib
434 rlca
435 rlca
436 rlca
437 rlca
438 and $0F
439 ld L,a
440 ld a,e
441 and $0F
442 inc a ;to adjust for decrement in the define.
443 ld e,a
444 pop af
445 push bc
446 call BoxDraw
447 pop bc
448 pop hl
449 djnz SetpuBoxesLoop
450 jp (hl)
451SetupBoxesLong:
452 ld a,c
453 and %01111111
454 ld c,a
455 push bc
456 push de
457; ld a,(hl)
458; inc hl
459 ld b,d ;temporary note.
460 ld e,(hl)
461 inc hl
462 ld d,(hl)
463 inc hl
464 push de
465 pop ix
466;C=x1 B=x2, DE is GAME right now.
467 ld a,(de)
468 dec b ;subtracting B for count
469 inc de
470 or a
471 jr nz,$-4
472 ld a,b
473 sub c
474 sub 2
475 pop de
476 pop bc
477 jr SetupBoxesCont