]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_loadcpm3.c
typo
[z180-stamp.git] / avr / cmd_loadcpm3.c
CommitLineData
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 */
30int 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
59command_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 FATFS FatFs;
69 FIL File;
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_mount(&FatFs, fname, 0);
91 if (!res)
92 res = f_open(&File, fname, FA_READ );
93 if (res) {
94 printf_P(PSTR("Error: failed to open '%s'\n"), fname);
95 f_mount(NULL, fname, 0);
96 return CMD_RET_FAILURE;
97 }
98
99 printf_P(PSTR("Loading: '%s'...\n"), fname);
100
101 /* read the load record */
102 res = f_read(&File, buffer, RS, &br);
103 if (res || br != RS)
104 goto out;
105
106 mem_top = buffer[0] << 8;
107 res_len = buffer[1];
108 bank_top = buffer[2] << 8;
109 bank_len = buffer[3];
110 osentry_adr = buffer[4] + (buffer[5] << 8);
111
112 /* read display info */
113 res = f_read(&File, buffer, RS, &br);
114 if (res || br != RS)
115 goto out;
116
117 /* print the info */
118 buffer[RS-1] = '$';
119 uint8_t *p = memchr(buffer, '$', RS);
120 *p = '\0';
121 my_puts((char *)buffer);
122
123 /* Main System Load */
124
125 /* Load Common Portion of System */
126 if ((res = load(&File, mem_top, res_len)) != 0)
127 goto out;
128
129 /* Load Banked Portion of System */
130 res = load(&File, bank_top, bank_len);
131
132out:
133 f_close(&File);
134 f_mount(NULL, fname, 0);
135
136 if (res) {
137 printf_P(PSTR("Error: failed to read '%s'\n"), fname);
138 return CMD_RET_FAILURE;
139 } else {
140 if (res_len != 0)
141 setenv_hex(PSTR(ENV_STARTADDRESS), osentry_adr);
142 printf_P(PSTR("Loaded: Resident: "));
143 if (res_len != 0)
144 printf_P(PSTR("0x%.4X-0x%.4X, "), mem_top-res_len*256, mem_top-1);
145 else
146 printf_P(PSTR(" - "));
147 printf_P(PSTR("Banked: "));
148 if (bank_len != 0)
149 printf_P(PSTR("0x%.4X-0x%.4X\n"), bank_top-bank_len*256, bank_top-1);
150 else
151 printf_P(PSTR(" - \n"));
152
153 return CMD_RET_SUCCESS;
154 }
155}