|
| 1 | +/* bootrom.c: Boot ROM simulator for Motorola processors |
| 2 | +
|
| 3 | + Copyright (c) 2010-2012, William A. Beech |
| 4 | +
|
| 5 | + Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | + copy of this software and associated documentation files (the "Software"), |
| 7 | + to deal in the Software without restriction, including without limitation |
| 8 | + the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | + and/or sell copies of the Software, and to permit persons to whom the |
| 10 | + Software is furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | + The above copyright notice and this permission notice shall be included in |
| 13 | + all copies or substantial portions of the Software. |
| 14 | +
|
| 15 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | + WILLIAM A. BEECH BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 19 | + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 | + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | +
|
| 22 | + Except as contained in this notice, the name of William A. Beech shall not be |
| 23 | + used in advertising or otherwise to promote the sale, use or other dealings |
| 24 | + in this Software without prior written authorization from William A. Beech. |
| 25 | +
|
| 26 | + The following copyright notice applies to the SWTP 6809 source, binary, and documentation: |
| 27 | + |
| 28 | + Original code published in 2024, written by Richard F Lukes |
| 29 | + Copyright (c) 2024, Richard F Lukes |
| 30 | + |
| 31 | + Permission is hereby granted, free of charge, to any person obtaining a |
| 32 | + copy of this software and associated documentation files (the "Software"), |
| 33 | + to deal in the Software without restriction, including without limitation |
| 34 | + the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 35 | + and/or sell copies of the Software, and to permit persons to whom the |
| 36 | + Software is furnished to do so, subject to the following conditions: |
| 37 | + |
| 38 | + The above copyright notice and this permission notice shall be included in |
| 39 | + all copies or substantial portions of the Software. |
| 40 | + |
| 41 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 42 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 43 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 44 | + THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 45 | + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 46 | + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 47 | + |
| 48 | + Except as contained in this notice, the names of The Authors shall not be |
| 49 | + used in advertising or otherwise to promote the sale, use or other dealings |
| 50 | + in this Software without prior written authorization from the Authors. |
| 51 | +
|
| 52 | + MODIFICATIONS: |
| 53 | +
|
| 54 | + 23 Apr 15 -- Modified to use simh_debug |
| 55 | + 04 Apr 24 -- Richard Lukes - Modified for swtp6809 emulator |
| 56 | +
|
| 57 | + NOTES: |
| 58 | +
|
| 59 | + These functions support a single simulated 2704 to 2764 EPROM device on |
| 60 | + an 8-bit computer system.. This device allows the buffer to be loaded from |
| 61 | + a binary file containing the emulated EPROM code. |
| 62 | +
|
| 63 | + These functions support a simulated 2704(0.5KB), 2708(1KB), 2716(2KB), 2732(4KB) or 2764(8KB) EPROM |
| 64 | + device at the top of the 16-bit address space. The base address of the ROM varies depends on the size. |
| 65 | + For example, The 2764 BOOTROM is mapped from $E000 to $FFFF less the I/O range reserved by the MP-B3 motherboard. |
| 66 | + The 2716 BOOTROM is mapped from $F800 to $FFFF. |
| 67 | + The last byte of the ROM is always stored at $FFFF |
| 68 | +
|
| 69 | + The device type is stored as a binary number in the first three unit flag bits. |
| 70 | +
|
| 71 | + This device uses a statically allocated buffer to hold the EPROM image. All bytes are initialized to $FF. |
| 72 | + A call to BOOTROM_attach will load the buffer with the EPROM image. |
| 73 | +
|
| 74 | +*/ |
| 75 | + |
| 76 | +#include <stdio.h> |
| 77 | +#include "swtp_defs.h" |
| 78 | + |
| 79 | +#define UNIT_V_MSIZE (UNIT_V_UF + 0) /* ROM Size */ |
| 80 | +#define UNIT_MSIZE (0x2F << UNIT_V_MSIZE) |
| 81 | +#define UNIT_NONE (0 << UNIT_V_MSIZE) /* No EPROM */ |
| 82 | +#define UNIT_2704 (1 << UNIT_V_MSIZE) /* 2704 mode */ |
| 83 | +#define UNIT_2708 (2 << UNIT_V_MSIZE) /* 2708 mode */ |
| 84 | +#define UNIT_2716 (3 << UNIT_V_MSIZE) /* 2716 mode */ |
| 85 | +#define UNIT_2732 (4 << UNIT_V_MSIZE) /* 2732 mode */ |
| 86 | +#define UNIT_2764 (5 << UNIT_V_MSIZE) /* 2764 mode */ |
| 87 | + |
| 88 | +/* Maximum size of bootrom is 8KB from $E000-$FFFF */ |
| 89 | +#define MAX_BOOTROM_SIZE (8*1024) |
| 90 | + |
| 91 | +// this value is used when referencing BOOTROM memory that is not populated (i.e. not loaded by BOOTROM attach) |
| 92 | +#define DEFAULT_NO_ROM_BYTE_VALUE 0xFF |
| 93 | + |
| 94 | +/* function prototypes */ |
| 95 | + |
| 96 | +t_stat BOOTROM_attach(UNIT *uptr, CONST char *cptr); |
| 97 | +t_stat BOOTROM_config(UNIT *uptr, int32 val, CONST char *cptr, void *desc); |
| 98 | +t_stat BOOTROM_examine(t_value *eval_array, t_addr addr, UNIT *uptr, int32 switches); |
| 99 | +t_stat BOOTROM_reset(DEVICE *dptr); |
| 100 | + |
| 101 | +int32 BOOTROM_get_mbyte(int32 address); |
| 102 | + |
| 103 | +/* SIMH Standard I/O Data Structures */ |
| 104 | + |
| 105 | +UNIT BOOTROM_unit = { |
| 106 | + UDATA (NULL, UNIT_ATTABLE+UNIT_BINK+UNIT_ROABLE+UNIT_RO, 0), |
| 107 | + KBD_POLL_WAIT }; |
| 108 | + |
| 109 | +MTAB BOOTROM_mod[] = { |
| 110 | + { UNIT_MSIZE, UNIT_NONE, "NONE", "NONE", &BOOTROM_config }, |
| 111 | + { UNIT_MSIZE, UNIT_2704, "2704", "2704", &BOOTROM_config }, |
| 112 | + { UNIT_MSIZE, UNIT_2708, "2708", "2708", &BOOTROM_config }, |
| 113 | + { UNIT_MSIZE, UNIT_2716, "2716", "2716", &BOOTROM_config }, |
| 114 | + { UNIT_MSIZE, UNIT_2732, "2732", "2732", &BOOTROM_config }, |
| 115 | + { UNIT_MSIZE, UNIT_2764, "2764", "2764", &BOOTROM_config }, |
| 116 | + { 0 } |
| 117 | +}; |
| 118 | + |
| 119 | +DEBTAB BOOTROM_debug[] = { |
| 120 | + { "ALL", DEBUG_all, "Debug all"}, |
| 121 | + { "FLOW", DEBUG_flow, "Debug flow of control" }, |
| 122 | + { "READ", DEBUG_read, "Debug device reads" }, |
| 123 | + { "WRITE", DEBUG_write, "Debug device writes" }, |
| 124 | + { "LEV1", DEBUG_level1, "Debug level 1" }, |
| 125 | + { "LEV2", DEBUG_level2, "Debug level 2" }, |
| 126 | + { NULL } |
| 127 | +}; |
| 128 | + |
| 129 | +DEVICE BOOTROM_dev = { |
| 130 | + "BOOTROM", /* name */ |
| 131 | + &BOOTROM_unit, /* units */ |
| 132 | + NULL, /* registers */ |
| 133 | + BOOTROM_mod, /* modifiers */ |
| 134 | + 1, /* numunits */ |
| 135 | + 16, /* aradix */ |
| 136 | + 16, /* awidth */ |
| 137 | + 1, /* aincr */ |
| 138 | + 16, /* dradix */ |
| 139 | + 8, /* dwidth */ |
| 140 | + &BOOTROM_examine, /* examine */ |
| 141 | + NULL, /* deposit */ |
| 142 | + &BOOTROM_reset, /* reset */ |
| 143 | + NULL, /* boot */ |
| 144 | + &BOOTROM_attach, /* attach */ |
| 145 | + NULL, /* detach */ |
| 146 | + NULL, /* ctxt */ |
| 147 | + DEV_DEBUG, /* flags */ |
| 148 | + 0, /* dctrl */ |
| 149 | + BOOTROM_debug, /* debflags */ |
| 150 | + NULL, /* msize */ |
| 151 | + NULL, /* help */ |
| 152 | + NULL, /* attach help */ |
| 153 | + NULL, /* help context */ |
| 154 | + NULL /* device description */ |
| 155 | +}; |
| 156 | + |
| 157 | +/* global variables */ |
| 158 | + |
| 159 | +/* MP-09 actually has 4 x 2KB EPROM sockets at $E000, $E800, $F000, $F800 */ |
| 160 | +/* This will be emulated as a single BOOTROM having a high address of $FFFF and variable start address depending on size */ |
| 161 | +/* Available sizes are None=0, 512B=2704, 1KB=2708, 2KB=2716, 4KB=2732, 8KB=2764 */ |
| 162 | + |
| 163 | +/* Pre-allocate 8KB array of bytes to accomodate largest BOOTROM */ |
| 164 | +uint8 BOOTROM_memory[MAX_BOOTROM_SIZE]; |
| 165 | + |
| 166 | +/* BOOTROM_examine routine */ |
| 167 | +t_stat BOOTROM_examine(t_value *eval_array, t_addr addr, UNIT *uptr, int32 switches) |
| 168 | +{ |
| 169 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_examine: addr=%08x\n", addr); |
| 170 | + |
| 171 | + if (addr >= uptr->capac || addr >= MAX_BOOTROM_SIZE) { |
| 172 | + return SCPE_NXM; |
| 173 | + } |
| 174 | + if (eval_array != NULL) { |
| 175 | + *eval_array = BOOTROM_memory[addr]; |
| 176 | + } |
| 177 | + return SCPE_OK; |
| 178 | + |
| 179 | +} /* BOOTROM_examine() */ |
| 180 | + |
| 181 | +/* BOOTROM_attach - attach file to EPROM unit */ |
| 182 | +t_stat BOOTROM_attach(UNIT *uptr, CONST char *cptr) |
| 183 | +{ |
| 184 | + t_stat r; |
| 185 | + t_addr image_size; |
| 186 | + int i,j; |
| 187 | + FILE *fp; |
| 188 | + uint8 byte_val; |
| 189 | + size_t items_read; |
| 190 | + |
| 191 | + if (BOOTROM_unit.filebuf == NULL) { /* no buffer allocated */ |
| 192 | + BOOTROM_unit.filebuf = BOOTROM_memory; |
| 193 | + } |
| 194 | + if ((BOOTROM_unit.flags & UNIT_MSIZE) == 0) { /* if none selected */ |
| 195 | + BOOTROM_unit.capac = 0; /* set EPROM size to 0 */ |
| 196 | + return SCPE_OK; |
| 197 | + } |
| 198 | + |
| 199 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_attach: cptr=%s\n", cptr); |
| 200 | + if ((r = attach_unit(uptr, cptr)) != SCPE_OK) { |
| 201 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_attach: Error %d\n", r); |
| 202 | + return r; |
| 203 | + } |
| 204 | + |
| 205 | + image_size = (t_addr) sim_fsize_ex(uptr->fileref); |
| 206 | + if (image_size <= 0) { |
| 207 | + sim_printf("BOOTROM_attach: File error\n"); |
| 208 | + detach_unit(uptr); |
| 209 | + return SCPE_IOERR; |
| 210 | + } else { |
| 211 | + if (image_size > MAX_BOOTROM_SIZE) { |
| 212 | + sim_printf("BOOTROM_attach: Error. File size exceeds ROM capacity\n"); |
| 213 | + detach_unit(uptr); |
| 214 | + return SCPE_ARG; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + /* open EPROM file */ |
| 219 | + fp = fopen(BOOTROM_unit.filename, "rb"); |
| 220 | + if (fp == NULL) { |
| 221 | + printf("Bootrom: Unable to open ROM file %s\n",BOOTROM_unit.filename); |
| 222 | + printf("Bootrom: No ROM image loaded!!!\n"); |
| 223 | + return SCPE_OK; |
| 224 | + } |
| 225 | + |
| 226 | + /* load EPROM file */ |
| 227 | + j = 0; |
| 228 | + items_read = sim_fread(&byte_val, (size_t)1, (size_t)1, fp); |
| 229 | + while (items_read != 0) { |
| 230 | + BOOTROM_memory[j++] = byte_val; |
| 231 | + items_read = sim_fread(&byte_val, (size_t)1, (size_t)1,fp); |
| 232 | + if (j > BOOTROM_unit.capac) { |
| 233 | + printf("Bootrom: Image is too large - Load truncated!!!\n"); |
| 234 | + j--; |
| 235 | + break; |
| 236 | + } |
| 237 | + } |
| 238 | + fclose(fp); |
| 239 | + printf("Bootrom: %d bytes of ROM image %s loaded\n", j, BOOTROM_unit.filename); |
| 240 | + |
| 241 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_attach: Done\n"); |
| 242 | + return SCPE_OK; |
| 243 | + |
| 244 | +} /* BOOTROM_attach() */ |
| 245 | + |
| 246 | +/* BOOTROM_config = None, 2704, 2708, 2716, 2732 or 2764 */ |
| 247 | +t_stat BOOTROM_config (UNIT *uptr, int32 val, CONST char *cptr, void *desc) |
| 248 | +{ |
| 249 | + |
| 250 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_config: val=%d\n", val); |
| 251 | + if ((val < UNIT_NONE) || (val > UNIT_2764)) { /* valid param? */ |
| 252 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_config: Parameter error\n"); |
| 253 | + return SCPE_ARG; |
| 254 | + } |
| 255 | + if (val == UNIT_NONE) { |
| 256 | + BOOTROM_unit.capac = 0; /* set EPROM size */ |
| 257 | + } else { |
| 258 | + BOOTROM_unit.capac = 0x100 << (val >> UNIT_V_MSIZE); /* set EPROM size */ |
| 259 | + } |
| 260 | + |
| 261 | + if (!BOOTROM_unit.filebuf) { /* point buffer to static array */ |
| 262 | + BOOTROM_unit.filebuf = BOOTROM_memory; |
| 263 | + } |
| 264 | + |
| 265 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_config: BOOTROM_unit.capac=%d\n", BOOTROM_unit.capac); |
| 266 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_config: Done\n"); |
| 267 | + |
| 268 | + return SCPE_OK; |
| 269 | +} /* BOOTROM config */ |
| 270 | + |
| 271 | +/* BOOTROM reset */ |
| 272 | +t_stat BOOTROM_reset (DEVICE *dptr) |
| 273 | +{ |
| 274 | + t_addr i; |
| 275 | + |
| 276 | + sim_debug(DEBUG_flow, &BOOTROM_dev, "BOOTROM_reset: \n"); |
| 277 | + |
| 278 | + /* allocate filebuf */ |
| 279 | + if (BOOTROM_unit.filebuf == NULL) { /* no buffer allocated */ |
| 280 | + //BOOTROM_unit.filebuf = calloc(1, BOOTROM_unit.capac); /* allocate EPROM buffer */ |
| 281 | + BOOTROM_unit.filebuf = BOOTROM_memory; |
| 282 | + if (BOOTROM_unit.filebuf == NULL) { |
| 283 | + return SCPE_MEM; |
| 284 | + } |
| 285 | + } |
| 286 | + return SCPE_OK; |
| 287 | + |
| 288 | +} /* BOOTROM_reset() */ |
| 289 | + |
| 290 | +/* get a byte from memory - from specified memory address */ |
| 291 | +int32 BOOTROM_get_mbyte(int32 address) |
| 292 | +{ |
| 293 | + int32 val; |
| 294 | + uint8 *pa; |
| 295 | + |
| 296 | + if (BOOTROM_unit.filebuf == NULL) { |
| 297 | + sim_debug(DEBUG_read, &BOOTROM_dev, "BOOTROM_get_mbyte: EPROM not configured\n"); |
| 298 | + return DEFAULT_NO_ROM_BYTE_VALUE; |
| 299 | + } |
| 300 | + sim_debug(DEBUG_read, &BOOTROM_dev, "BOOTROM_get_mbyte: address=%04X\n", address); |
| 301 | + if ((t_addr)(0xFFFF - address) > BOOTROM_unit.capac) { |
| 302 | + sim_debug(DEBUG_read, &BOOTROM_dev, "BOOTROM_get_mbyte: EPROM reference beyond ROM size\n"); |
| 303 | + return DEFAULT_NO_ROM_BYTE_VALUE; |
| 304 | + } |
| 305 | + |
| 306 | + pa = BOOTROM_unit.filebuf; |
| 307 | + /* the following code is needed to calculate offsets so address $FFFF references the last byte of the ROM */ |
| 308 | + val = DEFAULT_NO_ROM_BYTE_VALUE; |
| 309 | + switch (BOOTROM_unit.capac) { |
| 310 | + /* 2764 - $E000-$FFFF */ |
| 311 | + case 0x2000: |
| 312 | + val = pa[address - 0xE000]; |
| 313 | + break; |
| 314 | + /* 2732 - $F000-$FFFF */ |
| 315 | + case 0x1000: |
| 316 | + if (address >=0xF000) { |
| 317 | + val = pa[address - 0xF000]; |
| 318 | + } |
| 319 | + break; |
| 320 | + /* 2716 - $F800-$FFFF */ |
| 321 | + case 0x0800: |
| 322 | + if (address >= 0xF800) { |
| 323 | + val = pa[address - 0xF800]; |
| 324 | + } |
| 325 | + break; |
| 326 | + /* 2708 - $FC00-$FFFF */ |
| 327 | + case 0x0400: |
| 328 | + if (address >= 0xFC00) { |
| 329 | + val = pa[address - 0xFC00]; |
| 330 | + } |
| 331 | + break; |
| 332 | + /* 2704 - $FE00-$FFFF*/ |
| 333 | + case 0x0200: |
| 334 | + if (address >= 0xFE00) { |
| 335 | + val = pa[address - 0xFE00]; |
| 336 | + } |
| 337 | + break; |
| 338 | + default: |
| 339 | + break; |
| 340 | + } |
| 341 | + val &= 0xFF; |
| 342 | + sim_debug(DEBUG_read, &BOOTROM_dev, "BOOTROM_get_mbyte: Normal val=%02X\n", val); |
| 343 | + return val; |
| 344 | +} /* BOOTROM_get_mbyte() */ |
| 345 | + |
| 346 | +/* end of bootrom.c */ |
0 commit comments