1 /*
2  * Headerfile for Spectrum specific stuff
3  *
4  * $Id: spectrum.h,v 1.41 2016-06-11 19:53:08 dom Exp $
5  */
6 
7 #ifndef __SPECTRUM_H__
8 #define __SPECTRUM_H__
9 
10 #include <sys/compiler.h>
11 #include <sys/types.h>
12 
13 /////////////
14 // CONSTANTS
15 /////////////
16 
17 // Attributes
18 /*
19 #define BLACK          0x00
20 #define BLUE           0x01
21 #define RED            0x02
22 #define MAGENTA        0x03
23 #define GREEN          0x04
24 #define CYAN           0x05
25 #define YELLOW         0x06
26 #define WHITE          0x07
27 */
28 #define INK_BLACK      0x00
29 #define INK_BLUE       0x01
30 #define INK_RED        0x02
31 #define INK_MAGENTA    0x03
32 #define INK_GREEN      0x04
33 #define INK_CYAN       0x05
34 #define INK_YELLOW     0x06
35 #define INK_WHITE      0x07
36 
37 #define PAPER_BLACK    0x00
38 #define PAPER_BLUE     0x08
39 #define PAPER_RED      0x10
40 #define PAPER_MAGENTA  0x18
41 #define PAPER_GREEN    0x20
42 #define PAPER_CYAN     0x28
43 #define PAPER_YELLOW   0x30
44 #define PAPER_WHITE    0x38
45 
46 #define BRIGHT         0x40
47 #define FLASH          0x80
48 
49 // Basic Tokens
50 
51 #define TK_RND         165
52 #define TK_INKEYS      166
53 #define TK_PI          167
54 #define TK_FN          168
55 #define TK_POINT       169
56 #define TK_SCREENS     170
57 #define TK_ATTR        171
58 #define TK_AT          172
59 #define TK_TAB         173
60 #define TK_VALS        174
61 #define TK_CODE        175
62 #define TK_VAL         176
63 #define TK_LEN         177
64 #define TK_SIN         178
65 #define TK_COS         179
66 #define TK_TAN         180
67 #define TK_ASN         181
68 #define TK_ACS         182
69 #define TK_ATN         183
70 #define TK_LN          184
71 #define TK_EXP         185
72 #define TK_INT         186
73 #define TK_SQR         187
74 #define TK_SGN         188
75 #define TK_ABS         189
76 #define TK_PEEK        190
77 #define TK_IN          191
78 #define TK_USR         192
79 #define TK_STRS        193
80 #define TK_CHRS        194
81 #define TK_NOT         195
82 #define TK_BIN         196
83 #define TK_OR          197
84 #define TK_AND         198
85 #define TK_LEQ         199
86 #define TK_GEQ         200
87 #define TK_NEQ         201
88 #define TK_LINE        202
89 #define TK_THEN        203
90 #define TK_TO          204
91 #define TK_STEP        205
92 #define TK_DEF_FN      206
93 #define TK_CAT         207
94 #define TK_FORMAT      208
95 #define TK_MOVE        209
96 #define TK_ERASE       210
97 #define TK_OPEN        211
98 #define TK_CLOSE       212
99 #define TK_MERGE       213
100 #define TK_VERIFY      214
101 #define TK_BEEP        215
102 #define TK_CIRCLE      216
103 #define TK_INK         217
104 #define TK_PAPER       218
105 #define TK_FLASH       219
106 #define TK_BRIGHT      220
107 #define TK_INVERSE     221
108 #define TK_OVER        222
109 #define TK_OUT         223
110 #define TK_LPRINT      224
111 #define TK_LLIST       225
112 #define TK_STOP        226
113 #define TK_READ        227
114 #define TK_DATA        228
115 #define TK_RESTORE     229
116 #define TK_NEW         230
117 #define TK_BORDER      231
118 #define TK_CONTINUE    232
119 #define TK_CONT        232
120 #define TK_DIM         233
121 #define TK_REM         234
122 #define TK_FOR         235
123 #define TK_GO_TO       236
124 #define TK_GO_SUB      237
125 #define TK_INPUT       238
126 #define TK_LOAD        239
127 #define TK_LIST        240
128 #define TK_LET         241
129 #define TK_PAUSE       242
130 #define TK_NEXT        243
131 #define TK_POKE        244
132 #define TK_PRINT       245
133 #define TK_PLOT        246
134 #define TK_RUN         247
135 #define TK_SAVE        248
136 #define TK_RANDOMIZE   249
137 #define TK_RAND        249
138 #define TK_IF          250
139 #define TK_CLS         251
140 #define TK_DRAW        252
141 #define TK_CLEAR       253
142 #define TK_RETURN      254
143 #define TK_COPY        255
144 
145 
146 ///////////////////////////////////////////
147 // DIAGNOSTICS AND HARDWARE IDENTIFICATION
148 ///////////////////////////////////////////
149 
150 extern int  __LIB__ zx_128mode(void);            // true or false
151 extern int  __LIB__ zx_floatingbus(void);        // true or false
152 extern int  __LIB__ zx_issue3(void);             // true or false
153 extern int  __LIB__ zx_type(void);		 // 0:48K - 1: 128K - 2: TS2068
154 extern int  __LIB__ zx_model(void);
155 extern int  __LIB__ zx_basic_length(void);
156 extern int  __LIB__ zx_var_length(void);
157 extern int  __LIB__ zx_printer(void);
158 extern int  __LIB__ zx_soundchip(void);
159 extern int  __LIB__ zx_timexsound(void);
160 extern int  __LIB__ zx_fullerstick(void);
161 extern int  __LIB__ zx_kempstonmouse(void);
162 extern int  __LIB__ zx_kempston(void);
163 extern int  __LIB__ zx_iss_stick(void);
164 extern int  __LIB__ zx_mb02(void);
165 extern int  __LIB__ zx_multiface(void);
166 extern int  __LIB__ zx_disciple(void);
167 extern int  __LIB__ zx_plus3fdc(void);
168 extern int  __LIB__ zx_zxmmc(void);
169 extern int  __LIB__ zx_extsys(void);
170 extern int  __LIB__ zx_basemem(void);
171 extern int  __LIB__ zx_break(void);
172 extern int  __LIB__ zx_ulaplus(void);
173 
174 
175 /////////////////////////////////////////
176 // ULAPLUS/Arlequin palette mode project
177 /////////////////////////////////////////
178 
179 extern int  __LIB__ ula_plus_mode(void);
180 extern void __LIB__ ula_normal_mode(void);
181 extern void __LIB__ ula_sync(void);
182 extern int __LIB__ ulaplus_set(unsigned char attribute, unsigned char rgbcolor) __smallc;
183 extern int  __LIB__  ulaplus_get(unsigned char attribute) __z88dk_fastcall;
184 
185 
186 ///////////////////////////////
187 // INTERFACE FOR CALLING BASIC
188 ///////////////////////////////
189 
190 // Check syntax of an immediate BASIC command (e.g. {TK_CLS, '#', 13} would probe the Interface 1), max 20 bytes.
191 extern int  __LIB__  zx_syntax(char *statement) __z88dk_fastcall;
192 
193 // enter BASIC at a given program line, execution will stop gettin back to the
194 // calling C program when STOP or end of program will happen
195 extern int  __LIB__  zx_goto(int line) __z88dk_fastcall;
196 
197 // executes a single BASIC line (also TS2068 but does not work in 128k mode)
198 extern int  __LIB__  zx_line(int line) __z88dk_fastcall;
199 
200 // set/get string variable values, e.g. for A$: zx_setstr('a',"hello");
201 extern int  __LIB__              zx_getstr(char variable, char *value) __smallc;
202 extern void __LIB__              zx_setstr(char variable, char *value) __smallc;
203 
204 // set/get positive integer values in numeric variables
205 extern unsigned int  __LIB__  zx_getint(char *variable) __z88dk_fastcall;
206 extern void __LIB__              zx_setint(char *variable, unsigned int value) __smallc;
207 
208 // set/get FP values in numeric variables, e.g.  double a = zx_getfloat("number");
209 extern double_t __LIB__  zx_getfloat(char *variable) __z88dk_fastcall;
210 extern void __LIB__              zx_setfloat(char *variable, double_t value) __smallc;
211 
212 extern int  __LIB__    zx_getstr_callee(char variable, char *value) __smallc __z88dk_callee;
213 extern void __LIB__    zx_setstr_callee(char variable, char *value) __smallc __z88dk_callee;
214 extern void __LIB__    zx_setint_callee(char *variable, unsigned int value) __smallc __z88dk_callee;
215 extern void __LIB__    zx_setfloat_callee(char *variable, double_t value) __smallc __z88dk_callee;
216 
217 #define zx_getstr(a,b)           zx_getstr_callee(a,b)
218 #define zx_setstr(a,b)           zx_setstr_callee(a,b)
219 #define zx_setint(a,b)           zx_setint_callee(a,b)
220 #define zx_setfloat(a,b)         zx_setfloat_callee(a,b)
221 
222 
223 //////////////
224 // ZX PRINTER
225 //////////////
226 
227 extern void __LIB__  zx_lprintc(int chr);
228 extern void __LIB__  zx_hardcopy();
229 // Print out a 256 bytes buffer (8 rows)
230 extern void __LIB__  zx_print_buf(char *buf) __z88dk_fastcall;
231 // Print out a single graphics row (a 32 bytes buffer is required)
232 extern void __LIB__  zx_print_row(char *buf) __z88dk_fastcall;
233 
234 
235 ////////////
236 // TAPE I/O
237 ////////////
238 
239 struct zxtapehdr {             // standard tape header
240    unsigned char type;
241    char          name[10];
242    size_t        length;
243    size_t        address;
244    size_t        offset;
245 };
246 
247 extern int  __LIB__            tape_save(char *name, size_t loadstart,void *start, size_t len) __smallc;
248 extern int  __LIB__            tape_save_block(void *addr, size_t len, unsigned char type) __smallc;
249 extern int  __LIB__            tape_load_block(void *addr, size_t len, unsigned char type) __smallc;
250 
251 extern int  __LIB__  tape_save_block_callee(void *addr, size_t len, unsigned char type) __smallc __z88dk_callee;
252 extern int  __LIB__  tape_load_block_callee(void *addr, size_t len, unsigned char type) __smallc __z88dk_callee;
253 
254 #define tape_save_block(a,b,c) tape_save_block_callee(a,b,c)
255 #define tape_load_block(a,b,c) tape_load_block_callee(a,b,c)
256 
257 
258 ///////////////////////////////////
259 // DISK AND OTHER MASS STORAGE I/O
260 ///////////////////////////////////
261 
262 // RESIDOS
263 
264 #ifdef RESIDOS                 // if it's residos then we want dodos routines
265 #pragma output NEEDresidos
266 
267 #endif
268 
269 
270 /////////////////////////////////////////////////////////////////
271 // INPUT DEVICES: KEYBOARD, JOYSTICK AND MICE (SEE ALSO INPUT.H)
272 /////////////////////////////////////////////////////////////////
273 
274 // Joystick Functions
275 
276 extern unsigned int  __LIB__ in_JoyFuller(void);
277 extern unsigned int  __LIB__ in_JoyKempston(void);
278 extern unsigned int  __LIB__ in_JoySinclair1(void);
279 extern unsigned int  __LIB__ in_JoySinclair2(void);
280 extern unsigned int  __LIB__ in_JoyTimex1(void);
281 extern unsigned int  __LIB__ in_JoyTimex2(void);
282 
283 // AMX Mouse
284 //
285 // To use you must declare the following global variables
286 // uint in_AMXcoordX, in_AMXcoordY, in_AMXdeltaX, in_AMXdeltaY;
287 
288 extern void __LIB__             in_MouseAMXInit(uchar xvector, uchar yvector) __smallc;
289 extern void __LIB__             in_MouseAMX(uchar *buttons, uint *xcoord, uint *ycoord) __smallc;
290 extern void __LIB__             in_MouseAMXSetPos(uint xcoord, uint ycoord) __smallc;
291 
292 extern void __LIB__   in_MouseAMXInit_callee(uchar xvector, uchar yvector) __smallc __z88dk_callee;
293 extern void __LIB__   in_MouseAMX_callee(uchar *buttons, uint *xcoord, uint *ycoord) __smallc __z88dk_callee;
294 extern void __LIB__   in_MouseAMXSetPos_callee(uint xcoord, uint ycoord) __smallc __z88dk_callee;
295 
296 #define in_MouseAMXInit(a,b)    in_MouseAMXInit_callee(a,b)
297 #define in_MouseAMX(a,b,c)      in_MouseAMX_callee(a,b,c)
298 #define in_MouseAMXSetPos(a,b)  in_MouseAMXSetPos_callee(a,b)
299 
300 // Kempston Mouse
301 //
302 // To use you must declare the following global variables
303 // uchar in_KempcoordX, in_KempcoordY, in_KemprawX, in_KemprawY;
304 
305 extern void __LIB__             in_MouseKempInit(void);
306 extern void __LIB__             in_MouseKemp(uchar *buttons, uint *xcoord, uint *ycoord) __smallc;
307 extern void __LIB__             in_MouseKempSetPos(uint xcoord, uint ycoord) __smallc;
308 
309 extern void __LIB__   in_MouseKemp_callee(uchar *buttons, uint *xcoord, uint *ycoord) __smallc __z88dk_callee;
310 extern void __LIB__   in_MouseKempSetPos_callee(uint xcoord, uint ycoord) __smallc __z88dk_callee;
311 
312 #define in_MouseKemp(a,b,c)     in_MouseKemp_callee(a,b,c)
313 #define in_MouseKempSetPos(a,b) in_MouseKempSetPos_callee(a,b)
314 
315 
316 //////////////////////////
317 // DISPLAY FILE FUNCTIONS
318 //////////////////////////
319 
320 // NOTE: remember to compile with -pragma-define:CLIB_CONIO_NATIVE_COLOUR=1
321 // in order to use INK_BLACK to INK_WHITE and PAPER_BLACK to PAPER_WHITE.
322 // Clear the screen
323 #define zx_cls()                  fputc_cons(12)
324 
325 // Set or unset the flash attribute for now on
326 #define zx_setattrflash(b)        fputc_cons(18); fputc_cons((b)? 49: 48)
327 
328 // Set or unset the bright attribute for now on
329 #define zx_setattrbright(b)       fputc_cons(19); fputc_cons((b)? 49: 48)
330 
331 // Set or unset the inverse attribute for now on
332 #define zx_setattrinverse(b)      fputc_cons(20); fputc_cons((b)? 49: 48)
333 
334 // Set the border color
335 // Param colour can be any of: INK_BLACK, INK_BLUE,... to INK_WHITE
336 extern void  __LIB__  zx_border(uchar colour) __z88dk_fastcall;
337 // Quickly set the whole screen color attributes
338 // Param colour must be in the form i | p, where
339 // i can be any of: INK_BLACK, INK_BLUE,... to INK_WHITE
340 // p can be any of: PAPER_BLACK, PAPER_BLUE,... to PAPER_WHITE
341 extern void  __LIB__  zx_colour(uchar colour) __z88dk_fastcall;
342 // Change the paper attr from now on
343 // i can be any of: INK_BLACK, INK_BLUE,... to INK_WHITE
344 #define zx_setink(i) fputc_cons(16); fputc_cons(48 + i)
345 // Change the paper attr from now on
346 // p can be any of: INK_BLACK, INK_BLUE,... to INK_WHITE
347 #define zx_setpaper(p) fputc_cons(17); fputc_cons(48 + i)
348 // Get color attribute at given position
349 extern uint  __LIB__              zx_attr(uchar row, uchar col) __smallc;
350 extern uint  __LIB__              zx_screenstr(uchar row, uchar col) __smallc;
351 
352 extern uint  __LIB__    zx_attr_callee(uchar row, uchar col) __smallc __z88dk_callee;
353 extern uint  __LIB__    zx_screenstr_callee(uchar row, uchar col) __smallc __z88dk_callee;
354 
355 #define zx_attr(a,b)              zx_attr_callee(a,b)
356 #define zx_screenstr(a,b)         zx_screenstr_callee(a,b)
357 
358 // Sets the cursor position on screen
359 // Remember to add 32 to both the desired row & col.
360 #define zx_setcursorpos(row,col)      fputc_cons(22); fputc_cons(row); fputc_cons(col);
361 
362 // Sets the cursor position on screen
363 #define zx_movecursorto(row,col)      fputc_cons(22); fputc_cons(row + 32); fputc_cons(col + 32)
364 
365 #define zx_topleft()              fputc_cons(22); fputc_cons(0); fputc_cons(0);
366 
367 // Print a string on the screen
368 extern int __LIB__ zx_printf(char *fmt, ...);
369 
370 // In the following, screen address refers to a pixel address within the display file while
371 // attribute address refers to the attributes area.
372 //
373 // Function names are constructed from the following atoms:
374 //
375 // saddr = screen address
376 // aaddr = attribute address
377 //
378 // px    = pixel x coordinate (0..255)
379 // py    = pixel y coordinate (0..191)
380 // pxy   = pixel (x,y) coordinate
381 //
382 // cx    = character x coordinate (0..31)
383 // cy    = character y coordinate (0..23)
384 // cyx   = character (y,x) coordinate - ordering borrowed from Sinclair Basic
385 //
386 // So for example:
387 //
388 // zx_saddr2cy(saddr) will return the character y coordinate corresponding to the given screen address
389 // zx_saddr2aaddr(saddr) will return the attribute address corresponding to the given screen address
390 // zx_pxy2aaddr(px,py) will return the attribute address corresponding to the given (x,y) pixel coordinate
391 //
392 // Some functions will return with carry flag set if coordinates or addresses move out of
393 // bounds.  In these cases the special z88dk keywords iferror() and ifnerror() can be used
394 // to test the carry flag and determine if invalid results are returned.  Check with the
395 // wiki documentation or the asm source files to see which functions support this.  If
396 // comments in the asm source file do not mention this it is not supported.
397 
398 // DISPLAY PIXEL ADDRESS MANIPULATORS
399 
400 extern uchar __LIB__              *zx_cxy2saddr(uchar row, uchar col) __smallc;
401 extern uchar __LIB__  *zx_cy2saddr(uchar row) __z88dk_fastcall;           // cx assumed 0
402 
403 extern uchar __LIB__              *zx_pxy2saddr(uchar xcoord, uchar ycoord, uchar *mask) __smallc;
404 extern uchar __LIB__  *zx_py2saddr(uchar ycoord) __z88dk_fastcall;        // px assumed 0
405 
406 extern uint  __LIB__   zx_saddr2cx(void *pixeladdr) __z88dk_fastcall;
407 extern uint  __LIB__   zx_saddr2cy(void *pixeladdr) __z88dk_fastcall;
408 
409 extern uint  __LIB__               zx_saddr2px(void *pixeladdr, uchar mask) __smallc;
410 extern uint  __LIB__   zx_saddr2py(void *pixeladdr) __z88dk_fastcall;
411 
412 extern uchar __LIB__  *zx_saddr2aaddr(void *pixeladdr) __z88dk_fastcall;
413 
414 extern uchar __LIB__  *zx_saddrcdown(void *pixeladdr) __z88dk_fastcall;
415 extern uchar __LIB__  *zx_saddrcleft(void *pixeladdr) __z88dk_fastcall;
416 extern uchar __LIB__  *zx_saddrcright(void *pixeladdr) __z88dk_fastcall;
417 extern uchar __LIB__  *zx_saddrcup(void *pixeladdr) __z88dk_fastcall;
418 
419 extern uchar __LIB__  *zx_saddrpdown(void *pixeladdr) __z88dk_fastcall;
420 extern uchar __LIB__              *zx_saddrpleft(void *pixeladdr, uchar *mask) __smallc;
421 extern uchar __LIB__              *zx_saddrpright(void *pixeladdr, uchar *mask) __smallc;
422 extern uchar __LIB__  *zx_saddrpup(void *pixeladdr) __z88dk_fastcall;
423 
424 extern uchar __LIB__    *zx_cxy2saddr_callee(uchar row, uchar col) __smallc __z88dk_callee;
425 extern uchar __LIB__    *zx_pxy2saddr_callee(uchar xcoord, uchar ycoord, uchar *mask) __smallc __z88dk_callee;
426 extern uint  __LIB__     zx_saddr2px_callee(void *pixeladdr, uchar mask) __smallc __z88dk_callee;
427 extern uchar __LIB__    *zx_saddrpleft_callee(void *pixeladdr, uchar *mask) __smallc __z88dk_callee;
428 extern uchar __LIB__    *zx_saddrpright_callee(void *pixeladdr, uchar *mask) __smallc __z88dk_callee;
429 
430 #define zx_cyx2saddr(a,b)          zx_cxy2saddr_callee(b,a)
431 #define zx_cxy2saddr(a,b)          zx_cxy2saddr_callee(a,b)
432 
433 #define zx_pxy2saddr(a,b,c)        zx_pxy2saddr_callee(a,b,c)
434 #define zx_saddr2px(a,b)           zx_saddr2px_callee(a,b)
435 #define zx_saddrpleft(a,b)         zx_saddrpleft_callee(a,b)
436 #define zx_saddrpright(a,b)        zx_saddrpright_callee(a,b)
437 
438 // DISPLAY ATTRIBUTE ADDRESS MANIPULATORS
439 
440 extern uchar __LIB__              *zx_cxy2aaddr(uchar row, uchar col) __smallc;
441 extern uchar __LIB__  *zx_cy2aaddr(uchar row) __z88dk_fastcall;           // cx assumed 0
442 
443 extern uchar __LIB__              *zx_pxy2aaddr(uchar xcoord, uchar ycoord) __smallc;
444 extern uchar __LIB__  *zx_py2aaddr(uchar ycoord) __z88dk_fastcall;        // px assumed 0
445 
446 extern uint  __LIB__   zx_aaddr2cx(void *attraddr) __z88dk_fastcall;
447 extern uint  __LIB__   zx_aaddr2cy(void *attraddr) __z88dk_fastcall;
448 
449 extern uint  __LIB__   zx_aaddr2px(void *attraddr) __z88dk_fastcall;
450 extern uint  __LIB__   zx_aaddr2py(void *attraddr) __z88dk_fastcall;
451 
452 extern uchar __LIB__  *zx_aaddr2saddr(void *attraddr) __z88dk_fastcall;
453 
454 extern uchar __LIB__  *zx_aaddrcdown(void *attraddr) __z88dk_fastcall;
455 extern uchar __LIB__  *zx_aaddrcleft(void *attraddr) __z88dk_fastcall;
456 extern uchar __LIB__  *zx_aaddrcright(void *attraddr) __z88dk_fastcall;
457 extern uchar __LIB__  *zx_aaddrcup(void *attraddr) __z88dk_fastcall;
458 
459 extern uchar __LIB__    *zx_cxy2aaddr_callee(uchar row, uchar col) __smallc __z88dk_callee;
460 extern uchar __LIB__    *zx_pxy2aaddr_callee(uchar xcoord, uchar ycoord) __smallc __z88dk_callee;
461 
462 #define zx_cyx2aaddr(a,b)          zx_cxy2aaddr_callee(b,a)
463 #define zx_cxy2aaddr(a,b)          zx_cxy2aaddr_callee(a,b)
464 #define zx_pxy2aaddr(a,b)          zx_pxy2aaddr_callee(a,b)
465 
466 
467 /* Interrupt handling */
468 
469 #include <interrupt.h>
470 
471 // Setup an im2 jump table at given address
472 extern void __LIB__  zx_im2_init(void *address, uchar byte) __smallc;
473 
474 // Add a raster interrupt handler
475 extern void __LIB__ add_raster_int(isr_t handler);
476 
477 
478 /* This routine strips the drive specifier from the filename header.
479    It updates the 'D' BASIC variable with the drive number
480    and N$ with the file name  */
481 extern int  __LIB__  zxgetfname(char *zxfname);
482 extern int  __LIB__  zxgetfname2(char *zxfname);
483 
484 /* dirent */
485 struct dirent {
486     ino_t      d_ino;
487     char       d_name[12];
488 };
489 
490 #endif
491