]>
Commit | Line | Data |
---|---|---|
c0abd68b L |
1 | /* |
2 | * (C) Copyright 2015 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
7 | #include "common.h" | |
8 | #include <stdlib.h> | |
9 | #include <ctype.h> | |
10 | #include <string.h> | |
11 | #include <stdbool.h> | |
12 | ||
13 | #include "command.h" | |
14 | #include "env.h" | |
15 | #include "ff.h" | |
16 | #include "con-utils.h" | |
17 | #include "z80-if.h" | |
18 | #include "debug.h" | |
19 | ||
20 | ||
21 | #define RS 128 /* CP/M record size */ | |
22 | ||
23 | /* | |
24 | * Load Routine | |
25 | * | |
26 | * Input: addr = Page Address of load top | |
27 | * len = Length in pages of module to read | |
28 | * | |
29 | */ | |
30 | int load(FIL *File, uint16_t addr, uint8_t len) | |
31 | { | |
32 | uint8_t buffer[RS]; | |
33 | unsigned int br; /* bytes read */ | |
34 | int res; | |
35 | ||
36 | len *= 2; /* length in records of module */ | |
37 | //debug("## load: addr: 0x%.4X, records: 0x%.4X, (%u)\n", addr, len, len); | |
38 | ||
39 | for (; len; len--) { | |
40 | addr -= RS; | |
41 | res = f_read(File, buffer, RS, &br); | |
42 | if (res || br != RS) { | |
43 | return 1; | |
44 | } | |
45 | ||
46 | if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) { | |
47 | my_puts_P(PSTR("Bus timeout\n")); | |
48 | return 2; | |
49 | } | |
50 | z80_write_block(buffer, addr, RS); | |
51 | z80_bus_cmd(Release); | |
52 | //debug("## written: 0x%.4X\n", addr); | |
53 | } | |
54 | ||
55 | return 0; | |
56 | } | |
57 | ||
ad9bc17c | 58 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
c0abd68b L |
59 | |
60 | command_ret_t do_loadcpm3(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
61 | { | |
62 | uint16_t mem_top; | |
63 | uint8_t res_len; | |
64 | uint16_t bank_top; | |
65 | uint8_t bank_len; | |
66 | uint16_t osentry_adr = 0; | |
67 | long offset = 0; | |
68 | char *fname; | |
c0abd68b | 69 | FIL File; |
5f7f3586 | 70 | /* TODO: put CONFIG_PATH_CPM3SYS in flash */ |
c0abd68b L |
71 | char default_fname[] = CONFIG_PATH_CPM3SYS; |
72 | unsigned int br; /* bytes read */ | |
73 | uint8_t buffer[RS]; | |
74 | int res; | |
75 | ||
76 | (void) cmdtp; (void) flag; | |
77 | ||
78 | ||
79 | if (argc > 1) | |
80 | offset = strtoul(argv[1], NULL, 16); | |
81 | ||
82 | fname = getenv(PSTR(ENV_PATH_CPM3SYS)); | |
83 | ||
84 | if (argc > 2) { | |
85 | fname = argv[2]; | |
86 | } | |
87 | ||
88 | if (fname == NULL || *fname == '\0') | |
89 | fname = default_fname; | |
90 | ||
bbd45c46 | 91 | res = f_open(&File, fname, FA_READ ); |
c0abd68b L |
92 | if (res) { |
93 | printf_P(PSTR("Error: failed to open '%s'\n"), fname); | |
c0abd68b L |
94 | return CMD_RET_FAILURE; |
95 | } | |
96 | ||
97 | printf_P(PSTR("Loading: '%s'...\n"), fname); | |
98 | ||
99 | /* read the load record */ | |
100 | res = f_read(&File, buffer, RS, &br); | |
101 | if (res || br != RS) | |
102 | goto out; | |
103 | ||
104 | mem_top = buffer[0] << 8; | |
105 | res_len = buffer[1]; | |
106 | bank_top = buffer[2] << 8; | |
107 | bank_len = buffer[3]; | |
108 | osentry_adr = buffer[4] + (buffer[5] << 8); | |
109 | ||
110 | /* read display info */ | |
111 | res = f_read(&File, buffer, RS, &br); | |
112 | if (res || br != RS) | |
113 | goto out; | |
114 | ||
115 | /* print the info */ | |
116 | buffer[RS-1] = '$'; | |
117 | uint8_t *p = memchr(buffer, '$', RS); | |
118 | *p = '\0'; | |
119 | my_puts((char *)buffer); | |
120 | ||
121 | /* Main System Load */ | |
122 | ||
123 | /* Load Common Portion of System */ | |
124 | if ((res = load(&File, mem_top, res_len)) != 0) | |
125 | goto out; | |
126 | ||
127 | /* Load Banked Portion of System */ | |
128 | res = load(&File, bank_top, bank_len); | |
129 | ||
130 | out: | |
131 | f_close(&File); | |
c0abd68b L |
132 | |
133 | if (res) { | |
134 | printf_P(PSTR("Error: failed to read '%s'\n"), fname); | |
135 | return CMD_RET_FAILURE; | |
136 | } else { | |
137 | if (res_len != 0) | |
138 | setenv_hex(PSTR(ENV_STARTADDRESS), osentry_adr); | |
139 | printf_P(PSTR("Loaded: Resident: ")); | |
140 | if (res_len != 0) | |
141 | printf_P(PSTR("0x%.4X-0x%.4X, "), mem_top-res_len*256, mem_top-1); | |
142 | else | |
143 | printf_P(PSTR(" - ")); | |
144 | printf_P(PSTR("Banked: ")); | |
145 | if (bank_len != 0) | |
146 | printf_P(PSTR("0x%.4X-0x%.4X\n"), bank_top-bank_len*256, bank_top-1); | |
147 | else | |
148 | printf_P(PSTR(" - \n")); | |
149 | ||
150 | return CMD_RET_SUCCESS; | |
151 | } | |
152 | } |