1 /*  i2716.c: Intel 2716 EPROM simulator for 8-bit processors
2 
3     Copyright (c) 2011, 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     These functions support a simulated 2704 to 2764 EPROMs device on an 8-bit
27     computer system.  This device allows the attachment of the device to a binary file
28     containing the EPROM code.
29 
30     These functions support emulation of 0 to 4 2716 EPROM  devices on a CPU board.
31     The byte get and put routines use an offset into the boot EPROM image to locate
32     the proper byte.  This allows another device to set the base address for each
33     EPROM.
34 
35     This device uses a dynamically allocated buffer to hold each EPROM image.
36     A call to BOOTROM_config will free the current buffer.  A call to
37     i2716_reset will allocate a new buffer of 2048 bytes.  A
38     call to BOOTROM_attach will load the buffer with the EPROM image.
39 */
40 
41 #include <stdio.h>
42 #include "swtp_defs.h"
43 
44 #define I2716_NUM       4               /* number of 2716 EPROMS */
45 
46 extern  int32 get_base(void);
47 
48 /* function prototypes */
49 
50 t_stat i2716_attach (UNIT *uptr, char *cptr);
51 t_stat i2716_reset (DEVICE *dptr);
52 int32 i2716_get_mbyte(int32 offset);
53 
54 /* SIMH EPROM Standard I/O Data Structures */
55 
56 UNIT i2716_unit[] = {
57     { UDATA (NULL,UNIT_ATTABLE+UNIT_ROABLE+UNIT_RO, 0), 0 },
58     { UDATA (NULL,UNIT_ATTABLE+UNIT_ROABLE+UNIT_RO, 0), 0 },
59     { UDATA (NULL,UNIT_ATTABLE+UNIT_ROABLE+UNIT_RO, 0), 0 },
60     { UDATA (NULL,UNIT_ATTABLE+UNIT_ROABLE+UNIT_RO, 0), 0 }
61 };
62 
63 MTAB i2716_mod[] = {
64     { 0 }
65 };
66 
67 DEBTAB i2716_debug[] = {
68     { "ALL", DEBUG_all },
69     { "FLOW", DEBUG_flow },
70     { "READ", DEBUG_read },
71     { "WRITE", DEBUG_write },
72     { "LEV1", DEBUG_level1 },
73     { "LEV2", DEBUG_level2 },
74     { NULL }
75 };
76 
77 DEVICE i2716_dev = {
78     "I2716",                        /* name */
79     i2716_unit,                     /* units */
80     NULL,                           /* registers */
81     i2716_mod,                      /* modifiers */
82     I2716_NUM,                      /* numunits */
83     16,                             /* aradix */
84     32,                             /* awidth */
85     1,                              /* aincr */
86     16,                             /* dradix */
87     8,                              /* dwidth */
88     NULL,                           /* examine */
89     NULL,                           /* deposit */
90     &i2716_reset,                   /* reset */
91     NULL,                           /* boot */
92     &i2716_attach,                  /* attach */
93     NULL,                           /* detach */
94     NULL,                           /* ctxt */
95     DEV_DEBUG,                      /* flags */
96     0,                              /* dctrl */
97     i2716_debug,                    /* debflags */
98     NULL,                           /* msize */
99     NULL                            /* lname */
100 };
101 
102 /* global variables */
103 
104 /* i2716_attach - attach file to EPROM unit
105                   force EPROM reset at completion */
106 
i2716_attach(UNIT * uptr,char * cptr)107 t_stat i2716_attach (UNIT *uptr, char *cptr)
108 {
109     int32 j, c;
110     t_stat r;
111     FILE *fp;
112 
113     if (i2716_dev.dctrl & DEBUG_flow)
114         printf("i2716_attach: cptr=%s\n", cptr);
115     if ((r = attach_unit (uptr, cptr)) != SCPE_OK) {
116         if (i2716_dev.dctrl & DEBUG_flow)
117             printf("i2716_attach: Error\n");
118         return r;
119     }
120     if (i2716_dev.dctrl & DEBUG_read)
121         printf("\tOpen file\n");
122     fp = fopen(uptr->filename, "rb");   /* open EPROM file */
123     if (fp == NULL) {
124         printf("i2716%d: Unable to open ROM file %s\n", uptr - i2716_dev.units, uptr->filename);
125         printf("\tNo ROM image loaded!!!\n");
126         return SCPE_OK;
127     }
128     if (i2716_dev.dctrl & DEBUG_read)
129         printf("\tRead file\n");
130     j = 0;                              /* load EPROM file */
131     c = fgetc(fp);
132     while (c != EOF) {
133         *(uint8 *)(uptr->filebuf + j++) = c & 0xFF;
134         c = fgetc(fp);
135         if (j > 2048) {
136             printf("\tImage is too large - Load truncated!!!\n");
137             break;
138         }
139     }
140     if (i2716_dev.dctrl & DEBUG_read)
141         printf("\tClose file\n");
142     fclose(fp);
143 //    printf("i2716%d: %d bytes of ROM image %s loaded\n",uptr - i2716_dev.units, j, uptr->filename);
144     if (i2716_dev.dctrl & DEBUG_flow)
145         printf("i2716_attach: Done\n");
146     return SCPE_OK;
147 }
148 
149 /* EPROM reset */
150 
i2716_reset(DEVICE * dptr)151 t_stat i2716_reset (DEVICE *dptr)
152 {
153     int32 i, j, c, base;
154     t_stat r;
155     UNIT *uptr;
156 
157     if (i2716_dev.dctrl & DEBUG_flow)
158         printf("i2716_reset: \n");
159     for (i = 0; i < I2716_NUM; i++) {   /* init all units */
160         uptr = i2716_dev.units + i;
161         if (i2716_dev.dctrl & DEBUG_flow)
162             printf("i2716 %d unit.flags=%08X\n", i, uptr->flags);
163         uptr->capac = 2048;
164         uptr->u3 = 2048 * i;
165         base = get_base();
166         if (uptr->filebuf == NULL) {    /* no buffer allocated */
167             uptr->filebuf = malloc(2048); /* allocate EPROM buffer */
168             if (uptr->filebuf == NULL) {
169                 if (i2716_dev.dctrl & DEBUG_flow)
170                     printf("i2716_reset: Malloc error\n");
171                 return SCPE_MEM;
172             }
173         }
174         if (base == 0) {
175 //            printf("i2716%d: Not enabled on MP-A2\n", i);
176             continue;
177         }
178 //        printf("i2716%d: Initializing [%04X-%04XH]\n",
179 //            i, base+uptr->u3, base+uptr->u3 + uptr->capac);
180 //        if ((uptr->flags & UNIT_ATT) == 0) {
181 //            printf("i2716%d: No file attached\n", i);
182 //        }
183     }
184     if (i2716_dev.dctrl & DEBUG_flow)
185         printf("i2716_reset: Done\n");
186     return SCPE_OK;
187 }
188 
189 /*  I/O instruction handlers, called from the CPU module when an
190     EPROM memory read or write is issued.
191 */
192 
193 /*  get a byte from memory */
194 
i2716_get_mbyte(int32 offset)195 int32 i2716_get_mbyte(int32 offset)
196 {
197     int32 i, val, org, len;
198     UNIT *uptr;
199 
200     for (i = 0; i < I2716_NUM; i++) {   /* find addressed unit */
201         uptr = i2716_dev.units + i;
202         org = uptr->u3;
203         len = uptr->capac - 1;
204         if ((offset >= org) && (offset < (org + len))) {
205             if (uptr->filebuf == NULL) {
206                 if (i2716_dev.dctrl & DEBUG_read)
207                     printf("i2716_get_mbyte: EPROM not configured\n");
208                 return 0xFF;
209             } else {
210                 val = *(uint8 *)(uptr->filebuf + (offset - org));
211                 if (i2716_dev.dctrl & DEBUG_read)
212                     printf(" val=%04X\n", val);
213                 return (val & 0xFF);
214             }
215         }
216     }
217     if (i2716_dev.dctrl & DEBUG_read)
218         printf("i2716_get_mbyte: Out of range\n");
219     return 0xFF;
220 }
221 
222 /* end of i2716.c */
223