-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBLCRT.C
1237 lines (1065 loc) · 25.1 KB
/
BLCRT.C
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
*
* BLCRT: MSX-DOS2 Banked Application Loader
*
* 0100H~7FFFH: Banked Area
* 8000H~8FFFH: Banking Helper
* 9000H~93FFH: Internal shared buffer (1KBytes)
* 9400H~ : Shared Heap
*
* 100602 - First version
*
*********************************************************************/
#ifdef BL_ROM
#if BL_ROM == 32
#asm
psect text
defs 4000h ;offset for page1
#endasm
#endif
#asm
;-------------------------------------------------------------------------------
; Startup code for ROM loader
;
psect text
defs 100h
defm 'ROM ' ;ID
defw start
global start, _main, __Hbss, __Lbss, _gcBdosMode
start: ld de,__Lbss ;Start of BSS segment
or a ;clear carry
ld hl,__Hbss
sbc hl,de ;size of uninitialized data area
ld c,l
ld b,h
dec bc
ld l,e
ld h,d
inc de
ld (hl),0
ldir ;clear memory
jp _main
#endasm
#else /* BL_ROM */
#ifdef BL_DISABLE
#ifdef BL_DOS1
#asm
;-------------------------------------------------------------------------------
; Startup code for CP/M, MSX-DOS1, MSX-DOS2
;
psect text
defs 100h ;Base of CP/M's TPA
global start, _main, _exit_hl, __Hbss, __Lbss, __argc_, startup
global _GetBdosVersion, _gcBdosMode
start: ld hl,(6) ;base address of fdos
ld sp,hl ;stack grows downwards
ld de,__Lbss ;Start of BSS segment
or a ;clear carry
ld hl,__Hbss
sbc hl,de ;size of uninitialized data area
ld c,l
ld b,h
dec bc
ld l,e
ld h,d
inc de
ld (hl),0
ldir ;clear memory
ld hl,nularg
push hl
ld hl,80h ;argument buffer
ld c,(hl)
inc hl
ld b,0
add hl,bc
ld (hl),0 ;zero terminate it
ld hl,81h
push hl
call startup
pop bc ;unjunk stack
pop bc
push hl
ld hl,(__argc_)
push hl
; By Yeongman
; Check BDOS version
call _GetBdosVersion
ld a,h ; 0:CP/M, 1:MSX-DOS1, 2:MSX-DOS2
ld (_gcBdosMode),a
call _main
jp _exit_hl ; hl <- err no.
psect data
nularg: defb 0
#endasm
#else /* BL_DOS1 */
#asm
;-------------------------------------------------------------------------------
; Startup code for MSX-DOS2 Only
;
psect text
defs 100h ;Base of CP/M's TPA
global start, _main, _exit_hl, __Hbss, __Lbss, __argc_, startup, wrelop
start:
ld hl,(6) ;base address of fdos
ld sp,hl ;stack grows downwards
ld c,6fH ;Check MSX-DOS Version By Tatsu
call 0005H
or a
jp nz,notdos2
ld a,b
cp 2
jp c,notdos2
ld de,__Lbss ;Start of BSS segment
or a ;clear carry
ld hl,__Hbss
sbc hl,de ;size of uninitialized data area
ld c,l
ld b,h
dec bc
ld l,e
ld h,d
inc de
ld (hl),0
ldir ;clear memory
ld hl,nularg
push hl
ld hl,80h ;argument buffer
ld c,(hl)
inc hl
ld b,0
add hl,bc
ld (hl),0 ;zero terminate it
ld hl,81h
push hl
call startup
pop bc ;unjunk stack
pop bc
push hl
ld hl,(__argc_)
push hl
call _main
jp _exit_hl ;hl <- err no.
notdos2:
ld de,notdos2_mes
ld c,9
call 0005H
jp 0
notdos2_mes:
defm 'ERROR: MSX-DOS2 required'
defb 0dH,0aH,'$'
psect data
nularg: defb 0
#endasm
#endif /* BL_DOS1 */
#else /* BL_DISABLE */
/* #define BL_DEBUG */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <msxbdos.h>
#include <cpumode.h>
#include <blstd.h>
#include <blstdint.h>
#include <blstdmem.h>
#include <blstdslt.h>
/* Bank Call Routine */
#include <bankcall.h>
#ifdef BL_DEBUG
#define bl_dbg_pr(A) printf(A)
#define bl_dbg_pr_x(A, X) printf(A, X)
#else
#define bl_dbg_pr(A)
#define bl_dbg_pr_x(A, X)
#endif
#asm
;-------------------------------------------------------------------------------
; Startup code for MSX-DOS2 Only
;
psect text
defs 100h ;Base of CP/M's TPA
global start, _exit_hl, __Hbss, __Lbss, __argc_, startup, wrelop
start:
ld hl,(6) ;base address of fdos
ld sp,hl ;stack grows downwards
ld c,6fH ;Check MSX-DOS Version By Tatsu
call 0005H
or a
jp nz,notdos2
ld a,b
cp 2
jp c,notdos2
di ;disable interrupt
ld de,__Lbss ;Start of BSS segment
or a ;clear carry
ld hl,__Hbss
sbc hl,de ;size of uninitialized data area
ld c,l
ld b,h
dec bc
ld l,e
ld h,d
inc de
ld (hl),0
ldir ;clear memory
ld hl,BankCallBin
ld de,BankCall_entry
ld bc,BankCall_size
ldir ;transfer BankCall
call abort_init
call diskerr_init
ld hl,nularg
push hl
ld hl,80h ;argument buffer
ld c,(hl)
inc hl
ld b,0
add hl,bc
ld (hl),0 ;zero terminate it
ld hl,81h
push hl
call startup
pop bc ;unjunk stack
pop bc
push hl
ld hl,(__argc_)
push hl
ei ;enable interrupt
call _bl_main
push hl
call diskerr_deinit
call abort_deinit
pop hl
jp _exit_hl ;hl <- err no.
notdos2:
ld de,notdos2_mes
ld c,9
call 0005H
jp 0
notdos2_mes:
defm 'ERROR: MSX-DOS2 required'
defb 0dH,0aH,'$'
psect data
nularg: defb 0
#endasm
extern char seg_env_buf[];
extern char assert_msg[];
#asm
_seg_env_buf equ BankCallBin ; Recycling
_assert_msg equ 09000H
#endasm
static int16_t free_seg_no;
static uint8_t seg;
#ifndef BL_1BANK
struct bl_mem_seg_t { /* Memory Segment Info. */
short BankMax;
uint8_t BankTbl[64];
};
static struct bl_mem_seg_t tMemSeg;
static int8_t bl_tsr_mode = 0;
static uint8_t Page2Old;
static short SegCnt;
static short SegOvl;
static char pOvlName[64];
#ifdef BL_TSR
static unsigned char mem_seg_size = sizeof(tMemSeg);
static char pTsrEnvName[14]; /* ENV name */
static char *pTsrEnv = NULL; /* ENV data */
static int8_t bl_tsr_env_exist = 0;
void MakeTsrEnvName(void);
void put_seg_table(void);
void get_seg_table(void);
#endif
#endif
extern uint8_t bl_abort;
void bl_init_himem(void);
long _fsize(int fd);
uint8_t MapperInit(void);
uint8_t MapperAllocUser(void);
uint8_t MapperAllocSys(void);
void MapperFree(uint8_t SegNo);
uint8_t MapperGetPage0(void);
uint8_t MapperGetPage1(void);
uint8_t MapperGetPage2(void);
void MapperPutPage0(uint8_t SegNo);
void MapperPutPage1(uint8_t SegNo);
void MapperPutPage2(uint8_t SegNo);
void MakeOvlName(void);
void BankCallInit(void);
void ISRInit(void);
void ISRDeinit(void);
uint8_t OvlOpen(void);
void OvlClose(void);
short OvlGetBankMax(void);
void OvlLoad16kbPage2(uint8_t SegNo);
void copy_256_p0_to_p2(void);
void put_lmem_seg_table(struct bl_lmem_t *ptr);
void get_lmem_seg_table(struct bl_lmem_t *ptr);
int main(int argc, char *argv[]); /* main() */
static int ret_val;
int bl_main(int argc, char *argv[])
{
bl_init_himem(); /* Init himem */
#ifdef R800ONLY
if (get_msx_version() == MSXTR) {
set_cpu_mode_tr(CPU_TR_R800_DRAM);
} else {
puts("ERROR: MSXturboR required");
return 0;
}
#endif
free_seg_no = MapperInit();
bl_dbg_pr_x("[BL] Free seg = %d\n", free_seg_no);
#ifndef BL_1BANK
MakeOvlName(); /* Get Full Path of *.OVL */
#ifdef BL_TSR
MakeTsrEnvName(); /* Make Name of environment variable */
pTsrEnv = getenv(pTsrEnvName);
if (pTsrEnv && (pTsrEnv[0] == '*')) {
bl_dbg_pr_x("[BL] Load memory info [%s] ...", pTsrEnvName);
bl_tsr_mode = 1;
bl_tsr_env_exist = 1;
get_seg_table(); /* tMemSeg from pTsrEnv */
bl_dbg_pr("Ok\n");
}
#endif
if (!bl_tsr_mode) {
if (OvlOpen() == 0xFF) { /* Open OVL, Not Found? */
puts("ERROR: OVL not found");
return 0;
}
tMemSeg.BankMax = OvlGetBankMax();
#ifdef BL_BMAX
if (tMemSeg.BankMax > BL_BMAX) /* Partial loading? */
tMemSeg.BankMax = BL_BMAX;
#endif
OvlClose();
if (tMemSeg.BankMax * 2 > free_seg_no) {
tMemSeg.BankMax = bl_cb_adjust_BankMax(free_seg_no / 2);
if (tMemSeg.BankMax * 2 > free_seg_no) {
puts("ERROR: Not enough memory");
return 0;
}
}
bl_dbg_pr_x("[BL] Loading: %s\n", pOvlName);
}
#endif
/* Initialize Bank Caller */
BankCallInit();
#ifndef BL_1BANK
if (bl_tsr_mode) {
#asm
; memcpy(Bank_idx_addr + 0x02, tMemSeg.BankTbl, sizeof(tMemSeg.BankTbl));
LD HL, _tMemSeg + 2 ; HL = tMemSeg.BankTbl
LD DE, BankIndex_entry + 2 ; DE = Bank_idx_addr + 0x02
LD BC, 64 ; BC = sizeof(tMemSeg.BankTbl)
LDIR
#endasm
Page2Old = MapperGetPage2();
for (SegCnt = 0; SegCnt < tMemSeg.BankMax * 2; SegCnt += 2) {
MapperPutPage2(tMemSeg.BankTbl[SegCnt]); /* Set segment */
copy_256_p0_to_p2();
}
MapperPutPage2(Page2Old); /* Set original segment */
} else {
/* Load Banked code via Page2 */
OvlOpen(); /* re-open */
Page2Old = MapperGetPage2();
#ifdef BL_TSR
bl_dbg_pr("[BL] Allocate sys seg:");
#else
bl_dbg_pr("[BL] Allocate usr seg:");
#endif
for (SegCnt = 0; SegCnt < tMemSeg.BankMax * 2; SegCnt++, free_seg_no--) {
#ifdef BL_TSR
seg = MapperAllocSys(); /* Allocate system segment */
#else
seg = MapperAllocUser(); /* Allocate user segment */
#endif
bl_dbg_pr_x(" %02X", seg);
tMemSeg.BankTbl[SegCnt] = seg;
*(Bank_idx_addr + 0x02 + SegCnt) = seg;
OvlLoad16kbPage2(seg); /* Set segment & Load 16KB */
if (!(SegCnt & 0x01)) /* Page0 area?, Copy 0x0000~0x00FF */
copy_256_p0_to_p2();
MapperPutPage2(Page2Old); /* Set original segment */
}
OvlClose();
bl_dbg_pr("\n");
}
#endif
/* Disable OPLn Timer */
bl_opl_timer_off();
/* Install ISR */
ISRInit();
bl_dbg_pr_x("[BL] Free seg = %d\n", free_seg_no);
/* Backup regs for bl_exit() */
#asm
LD (main_ix), IX
LD (main_iy), IY
LD (main_sp), SP
#endasm
/* Enable Abort Handler */
bl_abort_on();
/* Execute main() function */
bl_dbg_pr("[BL] main() start\n");
ret_val = main(argc, argv);
#asm
main_ret:
#endasm
/* Disable Abort Handler */
bl_abort_off();
bl_dbg_pr_x("[BL] main() done (ret = %d)\n", ret_val);
/* Restore Original ISR */
ISRDeinit();
/* Assert message */
if (*assert_msg)
puts(assert_msg);
#ifndef BL_1BANK
#ifdef BL_TSR
if (bl_tsr_mode) {
if (!bl_tsr_env_exist) { /* TSR ENV not exist? */
bl_dbg_pr_x("[BL] Save memory info [%s] ...", pTsrEnvName);
seg_env_buf[0] = mem_seg_size;
put_seg_table(); /* tMemSeg to seg_env_buf */
setenv(pTsrEnvName, seg_env_buf);
bl_dbg_pr("Ok\n");
}
} else {
if (bl_tsr_env_exist)
setenv(pTsrEnvName, ""); /* Clear ENV */
/* Free all segment */
bl_dbg_pr("[BL] Release seg:");
for (SegCnt = 0; SegCnt < tMemSeg.BankMax * 2; SegCnt++, free_seg_no++) {
MapperFree(tMemSeg.BankTbl[SegCnt]);
bl_dbg_pr_x(" %02X", tMemSeg.BankTbl[SegCnt]);
}
bl_dbg_pr("\n");
}
#else
/* Free all segment */
bl_dbg_pr("[BL] Release seg:");
for (SegCnt = 0; SegCnt < tMemSeg.BankMax * 2; SegCnt++, free_seg_no++) {
MapperFree(tMemSeg.BankTbl[SegCnt]);
bl_dbg_pr_x(" %02X", tMemSeg.BankTbl[SegCnt]);
}
bl_dbg_pr("\n");
#endif
#endif
bl_dbg_pr_x("[BL] Free seg = %d\n", free_seg_no);
return ret_val;
}
#asm
;-------------------------------------------------------------------------------
; Workmem (9000H ~ 93FFH), Comm (9400H ~ )
;
;void bl_init_himem(void);
global _bl_init_himem
global __Hcomm, _brk
psect text
_bl_init_himem:
LD HL, __Hcomm
PUSH HL ; for brk()
LD DE, 09001H
XOR A
SBC HL, DE
LD B, H
LD C, L
LD HL, 09000H
LD (HL), 0
LDIR
CALL _brk
POP HL
RET
;void bl_exit(int n);
global _bl_exit
psect text
_bl_exit:
POP HL
POP HL
LD (_ret_val), HL
DEFB 0DDH, 021H ; LD IX, nn
main_ix: DEFW 0
DEFB 0FDH, 021H ; LD IY, nn
main_iy: DEFW 0
DEFB 031H ; LD SP, nn
main_sp: DEFW 0
JP main_ret ; return to bl_main()
;void bl_abort_on(void);
;void bl_abort_off(void);
global _bl_abort_on, _bl_abort_off, _callbdos_ixiy
psect text
_bl_abort_on:
LD A, 1
JR set_bl_abort
_bl_abort_off:
XOR A
set_bl_abort:
LD (_bl_abort), A
RET
abort_init:
LD HL, _bl_exit
LD (_ExitAddr), HL
LD DE, _AbortCallback
JR set_abort_cb
abort_deinit:
LD DE, 0
set_abort_cb:
LD C, 063H ; _DEFAB
JP _callbdos_ixiy
diskerr_init:
LD DE, _DiskECallback
JR set_diskerr_cb
diskerr_deinit:
LD DE, 0
set_diskerr_cb:
LD C, 064H ; _DEFER
JP _callbdos_ixiy
; bl_opl_timer_off(void);
global _bl_opl_timer_off
psect text
_bl_opl_timer_off:
DI
LD DE, 00418H
LD BC, 080C0H
CALL dis_opl_timer ; OPL C0H
CALL dis_opl_timer ; OPL C2H
; OPL C4H
dis_opl_timer:
OUT (C), D
INC C
OUT (C), E ; R04 = 18H (Disable T1, T2)
NOP
OUT (C), B ; Reset IRQ Flag
INC C
RET
#endasm
#ifndef BL_1BANK
static int fh_ovl;
uint8_t OvlOpen(void)
{
fh_ovl = open(pOvlName, O_RDONLY);
return (uint8_t)fh_ovl;
}
void OvlClose(void)
{
close(fh_ovl);
}
short OvlGetBankMax(void)
{
return (short)(_fsize(fh_ovl) >> 15);
}
void OvlLoad16kbPage2(uint8_t SegNo)
{
MapperPutPage2(SegNo);
read(fh_ovl, (uint8_t *)0x8000, 0x4000);
}
void bl_get_ovl_info(char *name, short *bank_max)
{
strcpy(name, pOvlName);
*bank_max = tMemSeg.BankMax;
}
uint16_t bl_get_seg_info(short bank)
{
return *((uint16_t *)Bank_idx_addr + bank);
}
void bl_overlay_seg(short dest_bank, short ovl_bank)
{
uint8_t seg0, seg1;
ISRDeinit();
SegOvl = (ovl_bank - 1) * 2;
SegCnt = (dest_bank - 1) * 2;
seg0 = tMemSeg.BankTbl[SegCnt];
seg1 = tMemSeg.BankTbl[SegCnt + 1];
lseek(OvlOpen(), (long)(ovl_bank - 1) * 0x8000, SEEK_SET);
OvlLoad16kbPage2(seg0);
copy_256_p0_to_p2();
OvlLoad16kbPage2(seg1);
MapperPutPage2(Page2Old);
OvlClose();
*(Bank_idx_addr + 0x02 + SegOvl) = seg0;
*(Bank_idx_addr + 0x03 + SegOvl) = seg1;
ISRInit();
}
void bl_tsr_on(void)
{
#ifdef BL_TSR
bl_tsr_mode = 1;
#endif
}
void bl_tsr_off(void)
{
bl_tsr_mode = 0;
}
int8_t bl_is_tsr_on(void)
{
return bl_tsr_mode;
}
#endif
uint32_t bl_lmem_get_free(void)
{
return (uint32_t)free_seg_no * 0x4000;
}
#asm
global _bl_lmem_alloc, _bl_lmem_alloc_sys
psect text
;struct bl_lmem_t *bl_lmem_alloc(uint32_t size)
_bl_lmem_alloc:
XOR A
JR bl_lmem_alloc_
;struct bl_lmem_t *bl_lmem_alloc_sys(uint32_t size)
_bl_lmem_alloc_sys:
LD A, 1
bl_lmem_alloc_:
LD (_lmem_sys_seg), A
JP _bl_lmem_alloc_do
#endasm
static uint8_t lmem_sys_seg;
struct bl_lmem_t *bl_lmem_alloc_do(uint32_t size)
{
static struct bl_lmem_t *lmem;
uint8_t page_no, n;
bl_dbg_pr_x("[BL] lmem alloc size = %lu bytes\n", size);
page_no = (uint8_t)((size + 0x3FFF) >> 14); /* N page */
if (!page_no || (page_no > free_seg_no)) {
return NULL;
}
lmem = (struct bl_lmem_t *)malloc(sizeof(struct bl_lmem_t));
if (lmem == NULL) { /* not enough heap */
return NULL;
}
free_seg_no -= page_no;
lmem->page_max = page_no;
lmem->sys_used = lmem_sys_seg;
bl_dbg_pr("[BL] seg:");
for (n = 0; n < page_no; n++) {
seg = lmem_sys_seg ? MapperAllocSys() : MapperAllocUser();
lmem->page_tbl[n] = seg;
bl_dbg_pr_x(" %02X", seg);
}
bl_dbg_pr_x("\n[BL] Free seg = %d\n", free_seg_no);
return lmem;
}
void bl_lmem_free(struct bl_lmem_t *ptr)
{
uint8_t n;
if (ptr) {
free_seg_no += ptr->page_max;
for (n = 0; n < ptr->page_max; n++) {
MapperFree(ptr->page_tbl[n]);
}
free(ptr);
bl_dbg_pr_x("[BL] Free seg = %d\n", free_seg_no);
}
}
uint8_t bl_lmem_get_seg(struct bl_lmem_t *ptr, uint32_t addr32)
{
return ptr->page_tbl[(uint8_t)(addr32 >> 14)];
}
void bl_lmem_export(struct bl_lmem_t *ptr, char *name)
{
if (ptr->sys_used) {
seg_env_buf[0] = ptr->page_max;
put_lmem_seg_table(ptr);
setenv(name, seg_env_buf);
}
}
struct bl_lmem_t *bl_lmem_import(char *name)
{
static struct bl_lmem_t *lmem;
static char *env;
lmem = (struct bl_lmem_t *)malloc(sizeof(struct bl_lmem_t));
if (lmem == NULL) { /* not enough heap */
return NULL;
}
env = getenv(name);
if (env[0] == '*') {
strcpy(seg_env_buf, env);
get_lmem_seg_table(lmem);
lmem->page_max = env[1] - 0x20;
lmem->sys_used = 1;
} else { /* env not found */
free(lmem);
return NULL;
}
return lmem;
}
void bl_lmem_copy_to(struct bl_lmem_t *dest, uint32_t addr32, uint8_t *src, uint16_t size)
{
uint8_t lmem_page_no, page1_seg;
uint16_t lmem_offset;
#asm
DI
#endasm
lmem_page_no = (uint8_t)(addr32 >> 14);
lmem_offset = ((uint16_t)addr32 & 0x3FFF) | 0x4000;
page1_seg = MapperGetPage1();
MapperPutPage1(dest->page_tbl[lmem_page_no]);
while (size--) {
*((uint8_t *)lmem_offset++) = *src++;
if ((lmem_offset == 0x8000) && (size > 1)) { /* End of mapped area? */
lmem_offset = 0x4000;
lmem_page_no++; /* for next mem page */
MapperPutPage1(dest->page_tbl[lmem_page_no]);
}
}
MapperPutPage1(page1_seg);
#asm
EI
#endasm
}
void bl_lmem_copy_from(uint8_t *dest, struct bl_lmem_t *src, uint32_t addr32, uint16_t size)
{
uint8_t lmem_page_no, page1_seg;
uint16_t lmem_offset;
#asm
DI
#endasm
lmem_page_no = (uint8_t)(addr32 >> 14);
lmem_offset = (((uint16_t)addr32) & 0x3FFF) | 0x4000;
page1_seg = MapperGetPage1();
MapperPutPage1(src->page_tbl[lmem_page_no]);
while (size--) {
*dest++ = *((uint8_t *)lmem_offset++);
if ((lmem_offset == 0x8000) && (size > 1)) { /* End of mapped area? */
lmem_offset = 0x4000;
lmem_page_no++; /* for next mem page */
MapperPutPage1(src->page_tbl[lmem_page_no]);
}
}
MapperPutPage1(page1_seg);
#asm
EI
#endasm
}
#ifndef BL_1BANK
#asm
;-------------------------------------------------------------------------------
; Fill pOvlName[]
;
;void MakeOvlName(void);
psect text
global _getenv, _strcpy_de_hl, _strlen_hl
_MakeOvlName:
LD HL, _str_program
PUSH HL
CALL _getenv ; HL <- full path
POP BC ; cleanup stack
LD DE, _pOvlName
CALL _strcpy_de_hl
CALL _strlen_hl ; HL <- length
LD DE, _pOvlName - 3
ADD HL, DE
LD (HL), 'O'
INC HL
LD (HL), 'V'
INC HL
LD (HL), 'L'
RET
_str_program: DEFM 'PROGRAM'
DEFB 0
#endasm
#endif
#asm
;-------------------------------------------------------------------------------
; Initialize Banking Helper Routine
;
;void BankCallInit(void);
_BankCallInit:
CALL _MapperGetPage0
LD E,L ; Page0 segment
CALL _MapperGetPage1
LD D,L ; Page1 segment
LD (BankIndex_entry),DE ; Set Bank0 table
_BankCallInit_P0a:
LD HL, 0 ; LD HL,(_MapperPUT_P0)
JP BankInit_entry
;-------------------------------------------------------------------------------
; Initialize Mapper Routine
;
;uint8_t MapperInit(void)
_MapperInit:
PUSH IX
LD A,000H
LD DE,0402H ; parameter
CALL 0FFCAH ; EXTBIO
LD (_MapperSegTotal),A ; Total number of segment
LD A,C
LD (_MapperSegFree),A ; Free segment number
LD (_MapperTblAddr),HL ; Base address
LD (_MapperAlloc_a + 1),HL ; ALL_SEG
LD DE,3
ADD HL,DE ; FRE_SEG
LD (_MapperFree_a + 1),HL
ADD HL,DE ; RD_SEG
ADD HL,DE ; WR_SEG
ADD HL,DE ; CAL_SEG
ADD HL,DE ; CALLS
ADD HL,DE ; PUT_PH
ADD HL,DE ; GET_PH
ADD HL,DE ; PUT_P0
LD (_BankCallInit_P0a + 1),HL
LD (_MapperPutPage0 + 1),HL
ADD HL,DE ; GET_P0
LD (_MapperGetPage0 + 1),HL
ADD HL,DE ; PUT_P1
LD (_MapperPutPage1 + 1),HL
ADD HL,DE ; GET_P1
LD (_MapperGetPage1 + 1),HL
ADD HL,DE ; PUT_P2
LD (_MapperPutPage2 + 1),HL
ADD HL,DE ; GET_P2
LD (_MapperGetPage2 + 1),HL
; ADD HL,DE ; PUT_P3
; ADD HL,DE ; GET_P3
POP IX
LD L,C ; return value (Free segment number)
RET
_MapperSegTotal:
DEFB 0
_MapperSegFree:
DEFB 0
_MapperTblAddr:
DEFW 0
;-------------------------------------------------------------------------------
; Allocate & Free Mapper Page
;
;uint8_t MapperAllocUser(void)
;uint8_t MapperAllocSys(void)
;void MapperFree(uint8_t SegNo)
_MapperAllocUser:
LD A,000H ; for user segment
LD B,000H ; for primary mapper
JR _MapperAlloc
_MapperAllocSys:
LD A,001H ; for system segment
LD B,000H ; for primary mapper
_MapperAlloc:
_MapperAlloc_a:
CALL 0 ; CALL ALL_SEG
LD H,0
LD L,A ; return value (allocated segment number)
RET NC
LD L,0FFH ; return value (Error = FFh)
RET
_MapperFree:
_MapperFree_hl: ; BLOPTIM fastcall
LD A,L ; SegNo
LD B,000H
_MapperFree_a:
JP 0 ; JP FRE_SEG
;-------------------------------------------------------------------------------
; Get Mapper Page