]> cloudbase.mooo.com Git - z180-stamp-cpm3.git/blob - cbios/mm.180
BIOS debugging with ddtz: Set 3F to 81 to en fifo inits.
[z180-stamp-cpm3.git] / cbios / mm.180
1 page 255
2 .z80
3
4
5 public mmuinit
6 public bnk2log,bnk2phy,hwl2phy,phy2log
7 public isv_sw
8 public b_ld_a,b_ld_hl,b_st_a,b_st_hl
9
10
11 extrn @cbnk
12 extrn ijphl
13
14
15 maclib z180reg.inc
16 maclib config.inc
17
18
19 ;----------------------------------------------------------------------
20 ; Memory Map 1:
21 ;
22 ; Common CAStart .. 0FFFF
23 ; Bank 0 00000 .. CAStart-1
24 ; Bank 1 10000 ..
25 ; Bank 2
26 ;
27 ; Memory Map 2:
28 ;
29 ; Common 18000 .. 1BFFF BANK1
30 ;
31 ; Bank 0 00000 .. 0BFFF 0
32 ; Bank 1 0C000 .. 17FFF 1*BNK_SIZE
33 ; Bank 2 1C000 .. 27FFF 2*BNK_SIZE + CMN_SIZE
34 ; Bank 3 28000 .. 33FFF 3*BNK_SIZE + CMN_SIZE
35 ; Bank n n*BNK_SIZE + (n < 2) ? 0 : CMN_SIZE
36 ;
37 ;----------------------------------------------------------------------
38
39 cseg
40
41 mmuinit:
42 ld a,USR$CBAR
43 out0 (cbar),a
44 ret
45
46 ;--------------------------------------------------------------------
47 ; Return the BBR value for the given bank number
48 ;
49 ; in a: Bank number
50 ; out a: bbr value
51
52 if 0 ; Memory Map 1
53
54 bnk2log:
55 or a ;
56 ret z ; Bank 0 is at physical address 0
57
58 dec a ;
59 push bc ;
60 ld c,a ;
61 ld b,BNK_SIZE ;
62 mlt bc ; bank size * bank number
63 ld a,c ;
64 add a,10h ; add bank0 + common
65 pop bc ;
66 ret ;
67
68 else ; Memory Map 2
69
70 bnk2log:
71 or a
72 ret z ; Bank 0 is at physical address 0
73
74 push bc
75 ld c,a ;
76 ld b,BNK_SIZE ;
77 mlt bc ; bank size * bank number
78 cp 2 ;
79 ld a,c ;
80 pop bc
81 ret c
82 add a,CMN_SIZE
83 ret
84
85 endif
86
87 if 0 ; table version
88
89 push hl
90 ld hl,bnk_table ;
91 add a,l ;
92 ld l,a ;
93 jr nc,$+3 ;
94 inc h ;
95 ld a,(hl) ;
96 pop hl
97 ret
98
99 endif
100
101 ;--------------------------------------------------------------
102
103 ;in hl: Log. Address
104 ; a: Bank number
105 ;
106 ;out ahl: Phys. (linear) Address
107
108
109 bnk2phy:
110 push bc
111 ld c,a
112 ld a,h
113 and a,0f0h
114 cp CA*16
115 ld a,c
116 pop bc
117 jr c,b2p_banked
118 ; address is in common
119 if 0 ; Memory Map 1
120 ld a,0 ; base is 0
121 else ; Memory Map 2
122 ld a,1 ; same as bank1
123 endif
124
125 b2p_banked:
126 call bnk2log ; get address base
127
128 ; fall thru
129
130 ;--------------------------------------------------------------
131 ;
132 ; hl: Log. Address
133 ; a: Bank base (bbr)
134 ;
135 ; 2 0 0
136 ; 0 6 8 0
137 ; hl hhhhhhhhllllllll
138 ; a + bbbbbbbb
139 ;
140 ; OP: ahl = (a<<12) + (h<<8) + l
141 ;
142 ;out ahl: Phys. (linear) Address
143
144 log2phy:
145 push bc ;
146 l2p_i:
147 ld c,a ;
148 ld b,16 ;
149 mlt bc ; bc = a<<4
150 ld a,c ;
151 add a,h ;
152 ld h,a ;
153 ld a,b ;
154 adc a,0 ;
155 pop bc ;
156 ret ;
157
158 ;--------------------------------------------------------------
159 ;
160 ; hl: Log. Address
161 ;
162 ;
163 ; OP: ahl = (bankbase<<12) + (h<<8) + l
164 ;
165 ;out ahl: Phys. (linear) Address
166
167
168 hwl2phy:
169 push bc ;
170 in0 c,(cbar) ;
171 ld a,h ;
172 or 00fh ; log. addr in common1?
173 cp c
174 jr c,hlp_1
175
176 in0 a,(cbr) ; yes, cbr is address base
177 jr hl2p_x
178 hlp_1:
179 ld b,16 ; log. address in baked area?
180 mlt bc
181 ld a,h
182 cp c
183 jr c,hlp_2
184 in0 a,(bbr) ; yes, bbr is address base
185 jr hl2p_x
186 hlp_2:
187 xor a ; common1
188 hl2p_x:
189 jr nz,l2p_i
190
191 pop bc ; bank part is 0, no translation
192 ret ;
193
194
195 ;--------------------------------------------------------------
196 ; return logical bank 0 address for given physical address.
197 ;
198 ; in: ahl: pyhsical addres (20 bit)
199 ; out hl: logical address.
200 ; logical address is in bank 0 or common, no bank number returned
201 ;
202
203 phy2log:
204 or a
205 ret z
206
207 push bc
208 push hl
209 ld l,h
210 ld h,0
211 ld bc,-16*SYS$CBR
212 add hl,bc
213 ld h,l
214 pop bc
215 ld l,c
216 pop bc
217 ret
218
219 ;--------------------------------------------------------------
220 ; Trampoline for routines in banked ram.
221 ; Switch stack pointer to "system" stack in top ram
222 ; Save cbar
223 ;
224 extrn bs$stack
225
226 cseg ; common!
227
228 public _b0call
229
230 if 0
231
232 _b0call:
233 push af
234 in0 a,(bbr)
235 jr nz,b0c_doit
236 pop af
237
238 ex (sp),hl ;16
239 push de
240 ld e,(hl)
241 inc hl
242 ld d,(hl)
243 inc hl
244 ld (b0c_fast_go),de
245 pop de
246 ex (sp),hl ;16
247 b0c_fast_go equ $+1
248 jp 0
249
250 b0c_doit:
251 ld (b0_save_hl),hl
252 ld (b0_save_de),de
253 pop hl
254 ld (b0_save_af),hl
255
256 pop de ;get ptr to 'function address' in de
257 ld hl,2
258 add hl,de
259 push hl ;put return address on stack
260
261 if 0 ; link80
262
263 ld hl,0
264 add hl,sp ;
265 ld a,h
266 cp high (bs$stack-bs$stack$size) ;link80 can't process this
267 jr nc,$ + 5 ;skip if stack allready in common bios ram
268
269 else
270
271 ld hl,bs$stack-bs$stack$size
272 ld a,h
273 dec a
274 ld hl,0
275 add hl,sp ;
276 cp h
277 jr c,$ + 5 ;skip if stack allready in common bios ram
278 endif
279 ld sp,bs$stack ;
280
281 push hl ;save user stack pointer
282
283 in0 h,(bbr) ;
284 push hl ;
285 ld hl,b0c_ret
286 push hl
287 xor a
288 out0 (bbr),a ;
289 ex de,hl ;ptr
290 ld e,(hl) ;get 'function address'
291 inc hl ;
292 ld d,(hl) ;
293 push de ;put on (switched) stack
294
295 ld hl,(b0_save_af) ;get back users registers
296 push hl
297 pop af
298 ld de,(b0_save_de)
299 ld hl,(b0_save_hl)
300 ret ;go to function
301 b0c_ret:
302 ld (b0_save_hl),hl
303
304 pop hl ;
305 out0 (bbr),h ;
306 pop hl ;
307 ld sp,hl ;
308 ld hl,(b0_save_hl)
309 ret ;
310 else
311
312 _b0call:
313 push af
314 ld (b0_save_hl),hl
315 ld (b0_save_de),de
316 pop hl
317 ld (b0_save_af),hl
318
319 pop de ;get ptr to 'function address' in de
320 ld hl,2
321 add hl,de
322 push hl ;put return address on stack
323
324 if 0 ; link80
325
326 ld hl,0
327 add hl,sp ;
328 ld a,h
329 cp high (bs$stack-bs$stack$size) ;link80 can't process this
330 jr nc,$ + 5 ;skip if stack allready in common bios ram
331
332 else
333
334 ld hl,bs$stack-bs$stack$size
335 ld a,h
336 dec a
337 ld hl,0
338 add hl,sp ;
339 cp h
340 jr c,$ + 5 ;skip if stack allready in common bios ram
341 endif
342 ld sp,bs$stack ;
343
344 push hl ;save user stack pointer
345
346 in0 h,(bbr) ;
347 push hl ;
348 ld hl,b0c_ret
349 push hl
350 xor a
351 out0 (bbr),a ;
352 ex de,hl ;ptr
353 ld e,(hl) ;get 'function address'
354 inc hl ;
355 ld d,(hl) ;
356 push de ;put on (switched) stack
357
358 ld hl,(b0_save_af) ;get back users registers
359 push hl
360 pop af
361 ld de,(b0_save_de)
362 ld hl,(b0_save_hl)
363 ret ;go to function
364 b0c_ret:
365 ld (b0_save_hl),hl
366
367 pop hl ;
368 out0 (bbr),h ;
369 pop hl ;
370 ld sp,hl ;
371 ld hl,(b0_save_hl)
372 ret ;
373 endif
374
375 b0_save_hl: dw 0
376 b0_save_de: dw 0
377 b0_save_af: dw 0
378
379
380 ;--------------------------------------------------------------------
381 ; Trampoline for interrupt routines in banked ram.
382 ; Switch stack pointer to "system" stack in top ram
383 ; Save bbr
384
385 cseg
386
387 if 0
388
389 isv_sw: ;
390 ex (sp),hl ;save hl, 'return adr' in hl
391 push de ;
392 push af ;
393 ex de,hl ;'return address' in de
394
395 if 0
396 if 0 ; link80
397
398 ld hl,0
399 add hl,sp ;
400 ld a,h
401 cp high (bs$stack-bs$stack$size) ;link80 can't process this
402 jr nc,$ + 5 ;skip if stack allready in common bios ram
403
404 else
405
406 ld hl,bs$stack-bs$stack$size
407 ld a,h
408 dec a
409 ld hl,0
410 add hl,sp ;
411 cp h
412 jr c,$ + 5 ;skip if stack allready in common bios ram
413 endif
414 ld sp,bs$stack ;
415 else
416 ld hl,0
417 add hl,sp
418 ld sp,istack
419 endif
420 push hl ;save user stack pointer
421 in0 h,(bbr) ;
422 push hl ;
423 xor a ;
424 out0 (bbr),a ;
425 ex de,hl ;
426 ld e,(hl) ;
427 inc hl ;
428 ld d,(hl) ;
429 ex de,hl ;
430 push bc ;
431 call ijphl ;
432
433 pop bc ;
434 pop hl ;
435 out0 (bbr),h ;
436 pop hl ;
437 ld sp,hl ;
438 pop af ;
439 pop de ;
440 pop hl ;
441 ei ;
442 ret ;
443
444 else
445
446 isv_sw: ;
447 ex (sp),hl ;save hl, 'return adr' in hl
448
449 ld (istack),sp ;save user stack pointer
450 ld sp,istack
451 push de ;
452 push bc ;
453 push af ;
454 in0 a,(bbr) ;
455 push af ;
456 xor a ;
457 out0 (bbr),a ;
458 ld e,(hl) ;
459 inc hl ;
460 ld d,(hl) ;
461 ex de,hl ;
462 call ijphl ;
463
464 pop af ;
465 out0 (bbr),a ;
466 pop af ;
467 pop bc ;
468 pop de ;
469 ld sp,(istack) ;
470 pop hl ;
471 ei ;
472 ret ;
473
474 endif
475 ds 24
476 istack:
477 dw 0
478
479 dseg
480
481 ;--------------------------------------------------------------------
482 ; Load byte/word from user ram
483 ;
484 ; de: src address in users bank
485 ; return
486 ; a: value (byte)
487 ; hl: value (word)
488
489 b_ld_a:
490 push hl
491 or a ; clear carry == byte store
492 jr $+3
493 b_ld_hl:
494 scf ; set carry == word store
495 push af ; save flag
496 push hl ; make space on stack
497
498 ld a,(@cbnk)
499 ld b,a ; b = src bank
500
501 ld hl,0
502 ld a,l
503 ld c,l ; c = dst bank (0)
504 add hl,sp ; hl = dst
505 adc a,1 ; a = count
506 ex de,hl
507 call dma_move
508 ex de,hl
509 pop hl
510 pop af
511 ret c
512 ld a,l
513 pop hl
514 ret
515
516 ;--------------------------------------------------------------------
517 ; Store byte/word to user ram
518 ;
519 ; de: dst address in users bank
520 ; a: value (byte)
521 ; hl: value (word)
522
523 b_st_a:
524 push hl
525 ld l,a
526 or a ; clear carry == byte store
527 jr $+3
528 b_st_hl:
529 scf ; set carry == word store
530 push af ; save flag
531 push hl ; put value on stack
532
533 ld a,(@cbnk) ;
534 ld c,a ; c = dst bank
535 ld a,0
536 ld l,a
537 ld h,a
538 ld b,a ; b = src bank (0)
539 add hl,sp ; hl = src
540 adc a,1 ; a = count
541
542 call dma_move
543
544 pop hl ; restore value
545 pop af ; carry
546 ret c
547 pop hl
548 ret
549
550 ;--------------------------------------------------------------------
551 ;
552 ; hl: src
553 ; de: dst
554 ; b: src bank
555 ; c: dst bank
556 ; a: count
557
558 dma_move:
559 out0 (bcr0l),a ; setup DMA count
560 xor a
561 out0 (bcr0h),a
562
563 push hl
564 ld a,b
565 call bnk2phy
566 out0 (sar0l),l ; setup DMA src address
567 out0 (sar0h),h
568 out0 (sar0b),a
569
570 ld l,e
571 ld h,d
572 ld a,c
573 call bnk2phy
574 out0 (dar0l),l ; setup DMA dst address
575 out0 (dar0h),h
576 out0 (dar0b),a
577
578 ld a,M_MMOD ; DMA burst mode
579 out0 (dmode),a
580 ld a,M_DE0+M_NDWE1 ; enable DMA0
581 out0 (dstat),a ; move ...
582 pop hl
583 ret
584
585
586 ;====================================================================
587
588 cseg
589
590 if 0
591
592 ;--------------------------------------------------------------------
593 ; Return the BBR value for the given bank number
594
595 bnk2bbr:
596 or a ; 4
597 ret z ; 5/10 | 11 14
598
599 push bc ;11 | 11
600 ld b,a ; 4
601 ld c,CA ; 6
602 mlt bc ;17 >45
603 ld a,c ; 4
604 add a,10h ; 6
605 pop bc ; 9 | 10
606 ret ; 9 | 10 76
607
608 push ix ;2 / 14 | 15
609 ld ix,bnktbl ;4 / 12 | 14
610 ld ($+3+2),a ;3 / 15 | 19
611 ld a,(ix+0) ;3 / 14 | 19
612 pop ix ;2 / 12 | 14
613 ret ;1 / 9 | 10 15 / 76|91
614
615 push hl ;1 / 11 | 11
616 ld hl,bnktbl ;3 / 9 | 10
617 add a,l ;1 / 4 | 4
618 ld l,a ;1 / 4 | 4
619 ld a,0 ;1 / 6 | 7
620 adc a,h ;1 / 4 | 4
621 ld h,a ;1 / 4 | 4
622 ld a,(hl) ;1 / 6 | 7
623 pop hl ;1 / 9 | 10
624 ret ;1 / 9 | 10 12 / 66|71
625
626 push hl ;1 / 11 | 11
627 add a,low bnktbl ;2 / 6 | 7
628 ld l,a ;1 / 4 | 4
629 ld a,0 ;1 / 6 | 7
630 adc a,high bnktbl ;2 / 6 | 7
631 ld h,a ;1 / 4 | 4
632 ld a,(hl) ;1 / 6 | 7
633 pop hl ;1 / 9 | 10
634 ret ;1 / 9 | 10 11 / 61|67
635
636 endif
637
638
639 end