1 /*
2  * (C) Copyright 2002, Brian Knittel.
3  * You may freely use this program, but: it offered strictly on an AS-IS, AT YOUR OWN
4  * RISK basis, there is no warranty of fitness for any purpose, and the rest of the
5  * usual yada-yada. Please keep this notice and the copyright in any distributions
6  * or modifications.
7  *
8  * This is not a supported product, but I welcome bug reports and fixes.
9  * Mail to sim@ibm1130.org
10  */
11 
12 /* ibm1130_defs.h: IBM-1130 simulator definitions
13  */
14 
15 #include "sim_defs.h"						/* main SIMH defns (include path should include .., or make a copy) */
16 #include "sim_console.h"					/* more SIMH defns (include path should include .., or make a copy) */
17 
18 #include <setjmp.h>
19 #include <assert.h>
20 #include <stdlib.h>
21 
22 #if defined(VMS)
23 	#  include <unistd.h>  					/* to pick up 'unlink' */
24 #endif
25 
26 #define MIN(a,b)  (((a) <= (b)) ? (a) : (b))
27 #define MAX(a,b)  (((a) >= (b)) ? (a) : (b))
28 
29 #ifndef _WIN32
30    int strnicmp (const char *a, const char *b, size_t n);
31    int strcmpi  (const char *a, const char *b);
32 #endif
33 
34 /* #define GUI_SUPPORT		uncomment to compile the GUI extensions. It's defined in the windows ibm1130.mak makefile */
35 
36 /* ------------------------------------------------------------------------ */
37 /* Architectural constants */
38 
39 #define MAXMEMSIZE	(32768)						/* 32Kwords */
40 #define INIMEMSIZE 	(16384)						/* 16Kwords */
41 #define MEMSIZE		(cpu_unit.capac)
42 
43 #define ILL_ADR_FLAG	0x40000000				/* an impossible 1130 address */
44 
45 /* ------------------------------------------------------------------------ */
46 /* Global state */
47 
48 extern int cgi;								/* TRUE if we are running as a CGI program */
49 extern int cgiwritable;						/* TRUE if we can write the disk images back to the image file in CGI mode */
50 extern t_bool sim_gui;
51 
52 extern uint16 M[];							/* core memory, up to 32Kwords (note: don't even think about trying 64K) */
53 extern uint16 ILSW[];						/* interrupt level status words */
54 extern int32  IAR;							/* instruction address register */
55 extern int32  prev_IAR;						/* instruction address register at start of current instruction */
56 extern int32  SAR, SBR;						/* storage address/buffer registers */
57 extern int32  OP, TAG, CCC;					/* instruction decoded pieces */
58 extern int32  CES;							/* console entry switches */
59 extern int32  ACC, EXT;						/* accumulator and extension */
60 extern int32  ARF;							/* arithmetic factor register, a nonaddressable internal CPU register */
61 extern int32  RUNMODE;						/* processor run/step mode */
62 extern int32  ipl;							/* current interrupt level (-1 = not handling irq) */
63 extern int32  iplpending;					/* interrupted IPL's */
64 extern int32  tbit;							/* trace flag (causes level 5 IRQ after each instr) */
65 extern int32  V, C;							/* condition codes */
66 extern int32  wait_state;					/* wait state (waiting for an IRQ) */
67 extern int32  wait_lamp;					/* alternate indicator to light the wait lamp on the GUI */
68 extern int32  int_req;						/* sum of interrupt request levels active */
69 extern int32  int_lamps;					/* accumulated version of int_req - gives lamp persistence */
70 extern int32  int_mask;						/* current active interrupt mask (ipl sensitive) */
71 extern int32  mem_mask;
72 extern int32  cpu_dsw;						/* CPU device status word */
73 extern int32  sim_int_char;					/* interrupt character */
74 extern int32  con_dsw;						/* has program stop and int run bits */
75 extern t_bool running;
76 extern t_bool power;
77 extern t_bool cgi;							/* TRUE if we are running as a CGI program */
78 extern t_bool cgiwritable;					/* TRUE if we can write to the disk image file in CGI mode */
79 extern t_stat reason;						/* CPU execution loop control */
80 
81 #define WAIT_OP			 1		/* wait state causes: wait instruction, invalid instruction*/
82 #define WAIT_INVALID_OP  2
83 
84 #define MODE_SS				3				/* RUNMODE values. SS and SMC are not implemented in this simulator */
85 #define MODE_SMC			2
86 #define MODE_INT_RUN		1
87 #define MODE_RUN			0
88 #define MODE_SI				-1
89 #define MODE_DISP			-2
90 #define MODE_LOAD			-3
91 
92 /* ------------------------------------------------------------------------ */
93 /* debugging																*/
94 /* ------------------------------------------------------------------------ */
95 
96 #define ENABLE_DEBUG_PRINT
97 #define ENABLE_DEBUG_TO_LOG
98 
99 #ifdef ENABLE_DEBUG_PRINT
100 #  define DEBUG_PRINT debug_print
101 #else
102 #  ifdef ENABLE_DEBUG_TO_LOG
103 #      define DEBUG_PRINT trace_io
104 #  else
105 #      define DEBUG_PRINT if (0) debug_print
106 #  endif
107 #endif
108 
109 void debug_print(char *fmt, ...);
110 
111 /* ------------------------------------------------------------------------ */
112 /* memory IO routines */
113 
114 int32 ReadW  (int32 a);
115 void  WriteW (int32 a, int32 d);
116 
117 /* ------------------------------------------------------------------------ */
118 /* handy macros */
119 
120 #define CLRBIT(v,b)    ((v) &= ~(b))
121 #define SETBIT(v,b)    ((v) |= (b))
122 #define BETWEEN(v,a,b) (((v) >= (a)) && ((v) <= (b)))
123 
124 /* ------------------------------------------------------------------------ */
125 /* Simulator stop codes */
126 
127 #define STOP_WAIT			1				/* wait, no events */
128 #define STOP_INVALID_INSTR	2				/* bad instruction */
129 #define STOP_IBKPT			3				/* simulator breakpoint */
130 #define STOP_INCOMPLETE		4				/* simulator coding not complete here */
131 #define STOP_POWER_OFF		5				/* no power */
132 #define STOP_DECK_BREAK		6				/* !BREAK in deck file */
133 #define STOP_PHASE_BREAK	7				/* phase load break */
134 #define STOP_CRASH			8				/* program has crashed badly */
135 #define STOP_TIMED_OUT		9				/* simulation time limit exceeded */
136 #define STOP_IMMEDIATE		10				/* simulator stop key pressed (immediate stop) */
137 #define STOP_BREAK			11				/* simulator break key pressed */
138 #define STOP_STEP			12				/* step count expired */
139 #define STOP_OTHER			13				/* other reason, probably error returned by sim_process_event() */
140 
141 #define IORETURN(f,v)	((f)? (v): SCPE_OK)	/* cond error return */
142 
143 #define INT_REQ_5		0x01				/* bits for interrupt levels (ipl, iplpending, int_req, int_mask) */
144 #define INT_REQ_4		0x02
145 #define INT_REQ_3		0x04
146 #define INT_REQ_2		0x08
147 #define INT_REQ_1		0x10
148 #define INT_REQ_0		0x20
149 
150 #define XIO_UNUSED		0x00				/* XIO commands */
151 #define XIO_WRITE		0x01
152 #define XIO_READ		0x02
153 #define XIO_SENSE_IRQ	0x03
154 #define XIO_CONTROL		0x04
155 #define XIO_INITW		0x05
156 #define XIO_INITR		0x06
157 #define XIO_SENSE_DEV	0x07
158 
159 #define XIO_FAILED		0x20				/* fake function to record error */
160 
161 /* ILSW bits - set by appropriate device whenever an interrupt is outstanding */
162 
163 #define ILSW_0_1442_CARD			0x8000			/* ILSW 0 is not really defined on the 1130 */
164 
165 #define ILSW_1_1132_PRINTER			0x8000			/* had these backwards! */
166 #define ILSW_1_SCA					0x4000
167 
168 #define ILSW_2_1131_DISK			0x8000
169 
170 #define ILSW_2_2310_DRV_1			0x4000
171 #define ILSW_2_2310_DRV_2			0x2000
172 #define ILSW_2_2310_DRV_3			0x1000
173 #define ILSW_2_2310_DRV_4			0x0800			/* can have 2310 or 2311 */
174 
175 #define ILSW_2_2311_DRV_1_DISK_1	0x4000
176 #define ILSW_2_2311_DRV_1_DISK_2	0x2000
177 #define ILSW_2_2311_DRV_1_DISK_3	0x1000
178 #define ILSW_2_2311_DRV_1_DISK_4	0x0800
179 
180 #define ILSW_2_2311_DRV_1_DISK_5	0x0400
181 #define ILSW_2_2311_DRV_2_DISK_1	0x0200
182 #define ILSW_2_2311_DRV_2_DISK_2	0x0100
183 #define ILSW_2_2311_DRV_2_DISK_3	0x0080
184 #define ILSW_2_2311_DRV_2_DISK_4	0x0040
185 #define ILSW_2_2311_DRV_2_DISK_5	0x0020
186 
187 #define ILSW_2_SAC_BIT_11			0x0010
188 #define ILSW_2_SAC_BIT_12			0x0008
189 #define ILSW_2_SAC_BIT_13			0x0004
190 #define ILSW_2_SAC_BIT_14			0x0002
191 #define ILSW_2_SAC_BIT_15			0x0001
192 
193 #define ILSW_3_1627_PLOTTER			0x8000
194 #define ILSW_3_SAC_BIT_01			0x4000
195 #define ILSW_3_SAC_BIT_02			0x2000
196 #define ILSW_3_SAC_BIT_03			0x1000
197 #define ILSW_3_2250_DISPLAY			0x0800
198 #define ILSW_3_SYSTEM7				0x0800
199 #define ILSW_3_SAC_BIT_05			0x0400
200 #define ILSW_3_SAC_BIT_06			0x0200
201 #define ILSW_3_SAC_BIT_07			0x0100
202 #define ILSW_3_SAC_BIT_08			0x0080
203 #define ILSW_3_SAC_BIT_09			0x0040
204 #define ILSW_3_SAC_BIT_10			0x0020
205 #define ILSW_3_SAC_BIT_11			0x0010
206 #define ILSW_3_SAC_BIT_12			0x0008
207 #define ILSW_3_SAC_BIT_13			0x0004
208 #define ILSW_3_SAC_BIT_14			0x0002
209 #define ILSW_3_SAC_BIT_15			0x0001
210 
211 #define ILSW_4_1134_TAPE			0x8000
212 #define ILSW_4_1055_TAPE			0x8000
213 #define ILSW_4_CONSOLE				0x4000
214 #define ILSW_4_1442_CARD			0x2000
215 #define ILSW_4_2501_CARD			0x1000
216 #define ILSW_4_1403_PRINTER			0x0800
217 #define ILSW_4_1231_MARK			0x0400
218 #define ILSW_4_SAC_BIT_06			0x0200
219 #define ILSW_4_SAC_BIT_07			0x0100
220 #define ILSW_4_SAC_BIT_08			0x0080
221 #define ILSW_4_SAC_BIT_09			0x0040
222 #define ILSW_4_SAC_BIT_10			0x0020
223 #define ILSW_4_SAC_BIT_11			0x0010
224 #define ILSW_4_T2741_TERMINAL		0x0010	/* APL\1130 nonstandard serial interface uses this bit */
225 #define ILSW_4_SAC_BIT_12			0x0008
226 #define ILSW_4_SAC_BIT_13			0x0004
227 #define ILSW_4_SAC_BIT_14			0x0002
228 #define ILSW_4_SAC_BIT_15			0x0001
229 
230 #define ILSW_5_INT_RUN_PROGRAM_STOP 0x8000	/* this replaces both ILSW_5_INT_RUN and ILSW_5_PROGRAM_STOP */
231 #define ILSW_5_SAC_BIT_01			0x4000
232 #define ILSW_5_SAC_BIT_02			0x2000
233 #define ILSW_5_SAC_BIT_03			0x1000
234 #define ILSW_5_SAC_BIT_04			0x0800
235 #define ILSW_5_SAC_BIT_05			0x0400
236 #define ILSW_5_SAC_BIT_06			0x0200
237 #define ILSW_5_SAC_BIT_07			0x0100
238 #define ILSW_5_SAC_BIT_08			0x0080
239 #define ILSW_5_SAC_BIT_09			0x0040
240 #define ILSW_5_SAC_BIT_10			0x0020
241 #define ILSW_5_SAC_BIT_11			0x0010
242 #define ILSW_5_SAC_BIT_12			0x0008
243 #define ILSW_5_SAC_BIT_13			0x0004
244 #define ILSW_5_SAC_BIT_14			0x0002
245 #define ILSW_5_SAC_BIT_15			0x0001
246 
247 /* CPU  DSW bits */
248 
249 #define CPU_DSW_PROGRAM_STOP			0x8000
250 #define CPU_DSW_INT_RUN					0x4000
251 
252 /* prototypes: xio handlers */
253 
254 void xio_1131_console	(int32 addr, int32 func, int32 modify);				/* console keyboard and printer */
255 void xio_1142_card		(int32 addr, int32 func, int32 modify);				/* standard card reader/punch */
256 void xio_1134_papertape	(int32 addr, int32 func, int32 modify);				/* paper tape reader/punch */
257 void xio_disk			(int32 addr, int32 func, int32 modify, int drv);	/* internal CPU disk */
258 void xio_1627_plotter	(int32 addr, int32 func, int32 modify);				/* XY plotter */
259 void xio_1132_printer	(int32 addr, int32 func, int32 modify);				/* standard line printer */
260 void xio_1131_switches	(int32 addr, int32 func, int32 modify);				/* console buttons & switches */
261 void xio_1231_optical	(int32 addr, int32 func, int32 modify);				/* optical mark page reader */
262 void xio_2501_card		(int32 addr, int32 func, int32 modify);				/* alternate high-speed card reader */
263 void xio_sca			(int32 addr, int32 func, int32 modify);				/* synchronous communications adapter */
264 void xio_system7		(int32 addr, int32 func, int32 modify);				/* system/7 interprocessor IO link */
265 void xio_1403_printer	(int32 addr, int32 func, int32 modify);				/* alternate high-speed printer */
266 void xio_2250_display	(int32 addr, int32 func, int32 modify);				/* vector display processor */
267 void xio_t2741_terminal (int32 addr, int32 func, int32 modify);				/* IO selectric via nonstandard serial interface for APL */
268 void xio_error 			(char *msg);
269 
270 void   bail (char *msg);
271 t_stat load_cr_boot (int drv, int switches);
272 t_stat cr_boot (int32 unitno, DEVICE *dptr);
273 t_stat cr_rewind (void);
274 t_stat cr_detach (UNIT *uptr);
275 void   calc_ints (void);							/* recalculate interrupt bitmask */
276 void   trace_io (char *fmt, ...);					/* debugging printout */
277 void   trace_both (char *fmt, ...);					/* debugging printout */
278 t_stat register_cmd (char *name, t_stat (*action)(int32 flag, char *ptr), int arg, char *help);
279 void   scp_panic (char *msg);						/* bail out of simulator */
280 char  *upcase(char *str);
281 void   break_simulation (t_stat reason);			/* let a device halt the simulation */
282 char   hollerith_to_ascii (uint16 hol);				/* for debugging use only */
283 t_bool gdu_active (void);
284 void   remark_cmd (char *remark);
285 void   stuff_cmd (char *cmd);
286 t_bool stuff_and_wait (char *cmd, int timeout, int delay);
287 void   update_gui (t_bool force);
288 void   sim_init (void);
289 t_stat register_cmd (char *name, t_stat (*action)(int32 flag, char *ptr), int arg, char *help);
290 t_stat basic_attach (UNIT *uptr, char *cptr);
291 char * quotefix (char * cptr);
292 
293 /* GUI interface routines */
294 t_bool keyboard_is_busy (void);
295 void   forms_check (int set);						/* device notification to console lamp display */
296 void   print_check (int set);
297 void   keyboard_selected (int select);
298 void   disk_ready (int ready);
299 void   disk_unlocked (int unlocked);
300 void   gui_run(int running);
301 char   *read_cmdline (char *ptr, int size, FILE *stream);
302 
303 #ifdef GUI_SUPPORT
304 #  define GUI_BEGIN_CRITICAL_SECTION begin_critical_section();
305 #  define GUI_END_CRITICAL_SECTION   end_critical_section();
306    void begin_critical_section (void);
307    void end_critical_section   (void);
308 #else
309 #  define GUI_BEGIN_CRITICAL_SECTION
310 #  define GUI_END_CRITICAL_SECTION
311 #endif
312