]>
Commit | Line | Data |
---|---|---|
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 | ||
58 | ||
59 | command_ret_t do_loadcpm3(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
60 | { | |
61 | uint16_t mem_top; | |
62 | uint8_t res_len; | |
63 | uint16_t bank_top; | |
64 | uint8_t bank_len; | |
65 | uint16_t osentry_adr = 0; | |
66 | long offset = 0; | |
67 | char *fname; | |
68 | FIL File; | |
69 | /* TODO: put CONFIG_PATH_CPM3SYS in flash */ | |
70 | char default_fname[] = CONFIG_PATH_CPM3SYS; | |
71 | unsigned int br; /* bytes read */ | |
72 | uint8_t buffer[RS]; | |
73 | int res; | |
74 | ||
75 | (void) cmdtp; (void) flag; | |
76 | ||
77 | ||
78 | if (argc > 1) | |
79 | offset = strtoul(argv[1], NULL, 16); | |
80 | ||
81 | fname = getenv(PSTR(ENV_PATH_CPM3SYS)); | |
82 | ||
83 | if (argc > 2) { | |
84 | fname = argv[2]; | |
85 | } | |
86 | ||
87 | if (fname == NULL || *fname == '\0') | |
88 | fname = default_fname; | |
89 | ||
90 | res = f_open(&File, fname, FA_READ ); | |
91 | if (res) { | |
92 | printf_P(PSTR("Error: failed to open '%s'\n"), fname); | |
93 | return CMD_RET_FAILURE; | |
94 | } | |
95 | ||
96 | printf_P(PSTR("Loading: '%s'...\n"), fname); | |
97 | ||
98 | /* read the load record */ | |
99 | res = f_read(&File, buffer, RS, &br); | |
100 | if (res || br != RS) | |
101 | goto out; | |
102 | ||
103 | mem_top = buffer[0] << 8; | |
104 | res_len = buffer[1]; | |
105 | bank_top = buffer[2] << 8; | |
106 | bank_len = buffer[3]; | |
107 | osentry_adr = buffer[4] + (buffer[5] << 8); | |
108 | ||
109 | /* read display info */ | |
110 | res = f_read(&File, buffer, RS, &br); | |
111 | if (res || br != RS) | |
112 | goto out; | |
113 | ||
114 | /* print the info */ | |
115 | buffer[RS-1] = '$'; | |
116 | uint8_t *p = memchr(buffer, '$', RS); | |
117 | *p = '\0'; | |
118 | my_puts((char *)buffer); | |
119 | ||
120 | /* Main System Load */ | |
121 | ||
122 | /* Load Common Portion of System */ | |
123 | if ((res = load(&File, mem_top, res_len)) != 0) | |
124 | goto out; | |
125 | ||
126 | /* Load Banked Portion of System */ | |
127 | res = load(&File, bank_top, bank_len); | |
128 | ||
129 | out: | |
130 | f_close(&File); | |
131 | ||
132 | if (res) { | |
133 | printf_P(PSTR("Error: failed to read '%s'\n"), fname); | |
134 | return CMD_RET_FAILURE; | |
135 | } else { | |
136 | if (res_len != 0) | |
137 | setenv_hex(PSTR(ENV_STARTADDRESS), osentry_adr); | |
138 | printf_P(PSTR("Loaded: Resident: ")); | |
139 | if (res_len != 0) | |
140 | printf_P(PSTR("0x%.4X-0x%.4X, "), mem_top-res_len*256, mem_top-1); | |
141 | else | |
142 | printf_P(PSTR(" - ")); | |
143 | printf_P(PSTR("Banked: ")); | |
144 | if (bank_len != 0) | |
145 | printf_P(PSTR("0x%.4X-0x%.4X\n"), bank_top-bank_len*256, bank_top-1); | |
146 | else | |
147 | printf_P(PSTR(" - \n")); | |
148 | ||
149 | return CMD_RET_SUCCESS; | |
150 | } | |
151 | } |