1 /*
2  * Copyright (c) 2019 Georg Brein. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 #ifndef TNYLPO_H
33 #define TNYLPO_H
34 
35 #include <stdlib.h>
36 #include <wchar.h>
37 
38 
39 /*
40  * log level
41  */
42 enum log_level {
43 	LL_UNSET = (-1) /* initial state */,
44 	LL_ERRORS = 0 /* report only errors */,
45 	LL_COUNTERS /* collect and output instruction counters */,
46 	LL_FDOS	/* trace FDOS functions */,
47 	LL_FCBS	/* dump FCBs in FDOS functions */,
48 	LL_RECORDS /* dumps data read and written */,
49 	LL_SYSCALL /* trace all OS functions */,
50 	LL_INVALID /* one log level too high... */
51 };
52 extern enum log_level log_level;
53 
54 
55 /*
56  * error messages, memory (re-)allocation
57  */
58 extern void perr(const char *format, ...);
59 extern void plog(const char *format, ...);
60 extern void plog_dump(int addr, int length);
61 extern void usage(void);
62 
63 
64 /*
65  * size of the Z80 main memory; base of the magic addresses for OS calls
66  * (the magic addresses are the last 19 addresses of the CP/M address
67  * space [ 0xffed ... 0xffff ]; an instruction fetch from one of these
68  * addresses causes a call to the emulated BDOS, to one of the emulated
69  * 17 CP/M BIOS entries, or to tnylpos delay routine)
70  */
71 #define MEMORY_SIZE (64 * 1024)
72 #define BIOS_VECTOR_COUNT 18
73 #define MAGIC_ADDRESS (MEMORY_SIZE - (1 + BIOS_VECTOR_COUNT))
74 
75 
76 /*
77  * parts of the CPU emulation visible to the OS emulation (the CP/M
78  * interface uses only 8080 compatible registers)
79  */
80 extern unsigned char *memory;
81 extern int reg_sp;
82 extern int reg_pc;
83 extern unsigned char reg_a;
84 extern unsigned char reg_b;
85 extern unsigned char reg_c;
86 extern unsigned char reg_d;
87 extern unsigned char reg_e;
88 extern unsigned char reg_h;
89 extern unsigned char reg_l;
90 
91 
92 /*
93  * flag for terminating emulator (set by an emulated OS function if
94  * it wants to terminate the emulation)
95  */
96 extern int terminate;
97 /*
98  * reason of terminating emulator
99  */
100 enum reason {
101 	OK_NOTRUN /* CPU emulation didn't rund due to earlier problems */,
102 	OK_TERM /* terminated by program (WBOOT, BDOS(0)) */,
103 	OK_CTRLC /* terminated by pressing ^C */,
104 	ERR_BOOT /* BOOT called (misbehaved program) */,
105 	ERR_BDOSARG /* illegal BDOS function parameter (misbehaved program) */,
106 	ERR_SELECT /* illegal/unconfigured disk drive accessed */,
107 	ERR_RODISK /* write access to R/O disk attempted */,
108 	ERR_ROFILE /* write access to R/O file attempted */,
109 	ERR_HOST /* host system call failed */,
110 	ERR_LOGIC  /* error in guest program logic */,
111 	ERR_SIGNAL /* caught a signal */,
112 	ERR_HALT /* HALT instruction executed */
113 };
114 extern enum reason term_reason;
115 
116 
117 /*
118  * CPU emulation functions
119  */
120 extern int cpu_init(void);
121 extern void cpu_run(void);
122 extern int cpu_exit(void);
123 
124 
125 /*
126  * OS emulation functions (in fact, OS emulation is part of the CPU
127  * emulation, but separated to keep the source file size managable)
128  */
129 extern int os_init(void);
130 extern void os_call(int magic);
131 extern int os_exit(void);
132 extern int get_tpa_end(void);
133 
134 
135 /*
136  * configuration from the command line and from the configuration file
137  */
138 extern int lines;
139 extern int cols;
140 extern int conf_interactive;
141 extern int altkeys;
142 extern int screen_delay;
143 extern wchar_t *conf_charset[256];
144 extern wchar_t *conf_alt_charset[256];
145 extern wchar_t *conf_unprintable;
146 extern char *conf_drives[16];
147 extern int conf_readonly[16];
148 extern char *conf_command;
149 extern int conf_argc;
150 extern char **conf_argv;
151 extern char *conf_printer;
152 extern int conf_printer_raw;
153 extern char *conf_punch;
154 extern int conf_punch_raw;
155 extern char *conf_reader;
156 extern int conf_reader_raw;
157 extern int charset;
158 extern char *conf_log;
159 extern int default_drive;
160 extern int dont_close;
161 extern int reverse_bs_del;
162 extern int delay_count;
163 extern int delay_nanoseconds;
164 extern int conf_color;
165 extern int conf_foreground;
166 extern int conf_background;
167 
168 
169 /*
170  * dump configuration
171  */
172 enum dump {
173 	DUMP_NONE = 0x01,
174 	DUMP_STARTUP = 0x02,
175 	DUMP_EXIT = 0x04,
176 	DUMP_ERROR = 0x08,
177 	DUMP_SIGNAL = 0x10,
178 	DUMP_ALL = 0x20
179 };
180 extern enum dump conf_dump;
181 
182 
183 /*
184  * save Z80 memory
185  */
186 extern const char *conf_save_file;
187 extern int conf_save_hex;
188 extern int conf_save_start;
189 extern int conf_save_end;
190 
191 
192 /*
193  * read the optional configuration file
194  */
195 extern int read_config(char *cfn);
196 
197 
198 /*
199  * utility functions
200  */
201 extern const char *base_name(const char *path);
202 extern void *alloc(size_t s);
203 extern void *resize(void *vp, size_t s);
204 
205 
206 /*
207  * character conversion
208  */
209 extern int to_cpm(wchar_t c);
210 extern wint_t from_cpm(unsigned char c);
211 extern wint_t from_graph(unsigned char c);
212 
213 
214 /*
215  * character I/O emulation
216  */
217 extern int console_init(void);
218 extern int console_exit(void);
219 extern unsigned char console_in(void);
220 extern void console_out(unsigned char c);
221 extern int console_status(void);
222 extern void console_poll(void);
223 extern void printer_out(unsigned char c);
224 extern int printer_status(void);
225 extern void punch_out(unsigned char c);
226 extern unsigned char reader_in(void);
227 extern int finalize_chario(void);
228 
229 
230 /*
231  * maximum and minimum sizes of the VT52 emulation
232  */
233 #define MIN_LINES 5
234 #define MAX_COLS 95
235 #define MIN_COLS 20
236 #define MAX_LINES 95
237 
238 
239 /*
240  * VT52 emulation (part of the character I/O emulation, but separated to
241  * keep source file sizes managable)
242  */
243 extern int crt_init(void);
244 extern void crt_exit(void);
245 extern unsigned char crt_in(void);
246 extern void crt_out(unsigned char c);
247 extern int crt_status(void);
248 extern void crt_poll(void);
249 
250 
251 #endif
252