]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_loadcpm3.c
2 * (C) Copyright 2015 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
8 * See CP/M 3 System Manual, Appendix D: CPM3.SYS File Format
20 #include "con-utils.h"
25 #define RS 128 /* CP/M record size */
32 * Input: addr = Page Address of load top
33 * len = Length in pages of module to read
36 int load(FIL
*File
, uint32_t addr
, uint8_t len
)
39 unsigned int br
; /* bytes read */
42 len
*= 2; /* length in records of module */
43 //debug("## load: addr: 0x%.4X, records: 0x%.4X, (%u)\n", addr, len, len);
47 res
= f_read(File
, buffer
, RS
, &br
);
48 if (res
|| br
!= RS
) {
52 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
53 my_puts_P(PSTR("Bus timeout\n"));
56 z80_write_block(buffer
, addr
, RS
);
58 //debug("## written: 0x%.4X\n", addr);
64 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
66 command_ret_t
do_loadcpm3(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
72 uint16_t osentry_addr
= 0;
73 uint32_t common_base
= 0;
77 char default_fname
[] = CONFIG_CPM3_SYSFILE
;
78 unsigned int br
; /* bytes read */
82 (void) cmdtp
; (void) flag
;
85 //common_base = getenv_ulong(PSTR(ENV_CPM3_COMMON_BASE), 16,
86 // CONFIG_CPM3_COMMON_BASE);
87 banked_base
= getenv_ulong(PSTR(ENV_CPM3_BANKED_BASE
), 16,
88 CONFIG_CPM3_BANKED_BASE
);
91 banked_base
= strtoul(argv
[3], NULL
, 16);
93 common_base
= strtoul(argv
[2], NULL
, 16);
95 fname
= getenv_char(PSTR(ENV_CPM3_SYSFILE
));
99 if (fname
== NULL
|| *fname
== '\0')
100 fname
= default_fname
;
102 res
= f_open(&File
, fname
, FA_READ
);
104 printf_P(PSTR("Error: failed to open '%s'\n"), fname
);
105 return CMD_RET_FAILURE
;
108 printf_P(PSTR("Loading: '%s'...\n"), fname
);
110 /* read the load record */
111 res
= f_read(&File
, buffer
, RS
, &br
);
115 mem_top
= buffer
[0] << 8;
117 bank_top
= buffer
[2] << 8;
118 bank_len
= buffer
[3];
119 osentry_addr
= buffer
[4] + (buffer
[5] << 8);
121 /* read display info */
122 res
= f_read(&File
, buffer
, RS
, &br
);
128 uint8_t *p
= memchr(buffer
, '$', RS
);
130 my_puts((char *)buffer
);
132 if (common_base
== 0) {
134 * http://www.seasip.info/Cpm/scb.html
136 FSIZE_t common_base_ofs
= ((res_len
- 6) << 8) + 2*RS
+ RS
-7;
137 FSIZE_t cur_pos
= f_tell(&File
);
138 if ((res
= f_lseek(&File
, common_base_ofs
)) ||
139 (res
= f_read(&File
, buffer
, 2, &br
)) ||
141 (res
= f_lseek(&File
, cur_pos
)))
143 common_base
= (uint16_t) buffer
[0] + (buffer
[1] << 8);
144 setenv_hex(PSTR(ENV_CPM3_COMMON_BASE
), common_base
);
147 setenv_hex(PSTR(ENV_CPM3_SCB
), mem_top
- ((res_len
- (6 - 1)) << 8) + common_base
);
149 /* Main System Load */
151 /* Load Common Portion of System */
152 if ((res
= load(&File
, common_base
+ mem_top
, res_len
)) != 0)
155 /* Load Banked Portion of System */
156 res
= load(&File
, banked_base
+ bank_top
, bank_len
);
162 printf_P(PSTR("Error: failed to read '%s'\n"), fname
);
163 return CMD_RET_FAILURE
;
166 if (osentry_addr
+ common_base
> 0xffff) {
167 z80_bus_cmd(Request
);
168 if (z80_read(osentry_addr
+ common_base
) == 0xc3) {
169 osentry_addr
= z80_read(osentry_addr
+common_base
+1) +
170 (z80_read(osentry_addr
+ common_base
+2) << 8);
172 z80_bus_cmd(Release
);
173 if (banked_base
+ osentry_addr
> 0xffff)
176 setenv_hex(PSTR(ENV_STARTADDRESS
), osentry_addr
);
178 printf_P(PSTR("Loaded: Resident: "));
180 printf_P(PSTR("0x%.5lX-0x%.5lX, "),
181 (common_base
+ mem_top
) - res_len
*256,
182 (common_base
+ mem_top
) - 1);
184 printf_P(PSTR(" - "));
185 printf_P(PSTR("Banked: "));
187 printf_P(PSTR("0x%.5lX-0x%.5lX\n"),
188 (banked_base
+ bank_top
) - bank_len
*256,
189 (banked_base
+ bank_top
) - 1);
191 printf_P(PSTR(" - \n"));
193 return CMD_RET_SUCCESS
;