1 /* Caprice32 - Amstrad CPC Emulator
2    (c) Copyright 1997-2005 Ulrich Doewich
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 #ifndef CAP32_H
20 #define CAP32_H
21 
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <stdbool.h>
30 #include <retro_endianness.h>
31 
32 //RETRO HACK
33 //TODO ENDIANNESS
34 //#define DEBUG 1
35 //#define DEBUG_CRTC
36 //#define DEBUG_CART
37 //#define DEBUG_ASIC
38 //#define DEBUG_FDC
39 //#define DEBUG_GA
40 //#define DEBUG_NO_VIDEO
41 //#define DEBUG_TAPE
42 //#define DEBUG_Z80
43 
44 #ifndef _MAX_PATH
45  #ifdef _POSIX_PATH_MAX
46  #define _MAX_PATH _POSIX_PATH_MAX
47  #else
48  #define _MAX_PATH 256
49  #endif
50 #endif
51 
52 #define CPC_BASE_FREQUENCY_MHZ 4.0
53 #define FRAME_PERIOD_MS        20.0
54 #define CYCLE_COUNT_INIT (CPC_BASE_FREQUENCY_MHZ*FRAME_PERIOD_MS*1000)
55 
56 #define CPC_SCR_WIDTH 1024 // max width
57 #define CPC_SCR_HEIGHT 312 // max height
58 #define CPC_VISIBLE_SCR_WIDTH 384 // visible width: 4+40+4 * 8
59 #define CPC_VISIBLE_SCR_HEIGHT 270
60 
61 #define CPC_MONITOR_COLOR 0
62 #define CPC_MONITOR_GREEN 1
63 #define CPC_MONITOR_WHITE 2
64 
65 #define ICN_DISK_WIDTH 14
66 #define ICN_DISK_HEIGHT 16
67 #define ICN_TAPE_WIDTH 18
68 #define ICN_TAPE_HEIGHT 13
69 
70 #define VOC_THRESHOLD 128
71 
72 // CRTC flags
73 #define VS_flag      1
74 #define HS_flag      2
75 #define HDT_flag     4
76 #define VDT_flag     8
77 #define HT_flag      16
78 #define VT_flag      32
79 #define MR_flag      64
80 #define VTadj_flag   128
81 #define VSf_flag     256
82 
83 // FDC constants
84 #define DSK_BPTMAX      8192
85 #define DSK_TRACKMAX    102   // max amount that fits in a DSK header
86 #define DSK_SIDEMAX     2
87 #define DSK_SECTORMAX   29    // max amount that fits in a track header
88 
89 #define FDC_TO_CPU      0
90 #define CPU_TO_FDC      1
91 
92 #define CMD_PHASE       0
93 #define EXEC_PHASE      1
94 #define RESULT_PHASE    2
95 
96 #define SKIP_flag       1     // skip sectors with DDAM/DAM
97 #define SEEKDRVA_flag   2     // seek operation has finished for drive A
98 #define SEEKDRVB_flag   4     // seek operation has finished for drive B
99 #define RNDDE_flag      8     // simulate random DE sectors
100 #define OVERRUN_flag    16    // data transfer timed out
101 #define SCAN_flag       32    // one of the three scan commands is active
102 #define SCANFAILED_flag 64    // memory and sector data does not match
103 #define STATUSDRVA_flag 128   // status change of drive A
104 #define STATUSDRVB_flag 256   // status change of drive B
105 
106 // Multiface 2 flags
107 #define MF2_ACTIVE      1
108 #define MF2_RUNNING     2
109 #define MF2_INVISIBLE   4
110 
111 
112 
113 typedef struct {
114    char id[8+1];
115    char unused1[8];
116    unsigned char version;
117    unsigned char AF[2];
118    unsigned char BC[2];
119    unsigned char DE[2];
120    unsigned char HL[2];
121    unsigned char R;
122    unsigned char I;
123    unsigned char IFF0;
124    unsigned char IFF1;
125    unsigned char IX[2];
126    unsigned char IY[2];
127    unsigned char SP[2];
128    unsigned char PC[2];
129    unsigned char IM;
130    unsigned char AFx[2];
131    unsigned char BCx[2];
132    unsigned char DEx[2];
133    unsigned char HLx[2];
134    unsigned char ga_pen;
135    unsigned char ga_ink_values[17];
136    unsigned char ga_ROM_config;
137    unsigned char ga_RAM_config;
138    unsigned char crtc_reg_select;
139    unsigned char crtc_registers[18];
140    unsigned char upper_ROM;
141    unsigned char ppi_A;
142    unsigned char ppi_B;
143    unsigned char ppi_C;
144    unsigned char ppi_control;
145    unsigned char psg_reg_select;
146    unsigned char psg_registers[16];
147    unsigned char ram_size[2];
148 // version 2 info follows
149    unsigned char cpc_model;
150    unsigned char last_interrupt;
151    unsigned char scr_modes[6];
152 // version 3 info follows
153    unsigned char drvA_DOSfilename[13];
154    unsigned char drvB_DOSfilename[13];
155    unsigned char cart_DOSfilename[13];
156    unsigned char fdc_motor;
157    unsigned char drvA_current_track;
158    unsigned char drvB_current_track;
159    unsigned char drvC_current_track;
160    unsigned char drvD_current_track;
161    unsigned char printer_data;
162    unsigned char psg_env_step;
163    unsigned char psg_env_direction;
164    unsigned char crtc_type;
165    unsigned char crtc_addr[2];
166    unsigned char crtc_scanline[2];
167    unsigned char crtc_char_count[2];
168    unsigned char crtc_line_count;
169    unsigned char crtc_raster_count;
170    unsigned char crtc_vt_adjust_count;
171    unsigned char crtc_hsw_count;
172    unsigned char crtc_vsw_count;
173    unsigned char crtc_flags[2];
174    unsigned char ga_int_delay;
175    unsigned char ga_sl_count;
176    unsigned char z80_int_pending;
177    unsigned char unused2[75];
178 } t_SNA_header;
179 
180 typedef struct {
181    char id[34];
182    char unused1[14];
183    unsigned char tracks;
184    unsigned char sides;
185    unsigned char unused2[2];
186    unsigned char track_size[DSK_TRACKMAX*DSK_SIDEMAX];
187 } t_DSK_header;
188 
189 typedef struct {
190    char id[12];
191    char unused1[4];
192    unsigned char track;
193    unsigned char side;
194    unsigned char unused2[2];
195    unsigned char bps;
196    unsigned char sectors;
197    unsigned char gap3;
198    unsigned char filler;
199    unsigned char sector[DSK_SECTORMAX][8];
200 } t_track_header;
201 
202 
203 
204 typedef struct {
205    unsigned int model;
206    unsigned int jumpers;
207    unsigned int ram_size;
208    unsigned int speed;
209    unsigned int limit_speed;
210    unsigned int paused;
211    unsigned int auto_pause;
212    unsigned int keyboard_line;
213    unsigned int tape_motor;
214    unsigned int tape_play_button;
215    unsigned int printer;
216    unsigned int printer_port;
217    unsigned int mf2;
218    unsigned int keyboard;
219    unsigned int joysticks;
220    int cycle_count;
221 
222    unsigned int scr_fs_width;
223    unsigned int scr_fs_height;
224    unsigned int scr_fs_bpp;
225    unsigned int scr_style;
226    unsigned int scr_oglfilter;
227    unsigned int scr_vsync;
228    unsigned int scr_led;
229    unsigned int scr_fps;
230    unsigned int scr_tube;
231    unsigned int scr_intensity;
232    unsigned int scr_phosphor_intensity;
233    unsigned int scr_remanency;
234    unsigned int scr_window;
235    unsigned int scr_bpp;
236    unsigned int scr_bps;
237    unsigned int scr_line_offs;
238    unsigned int *scr_base;
239    unsigned int *scr_pos;
240    void (*scr_render)(void);
241    void (*scr_prerendernorm)(void);
242    void (*scr_prerenderbord)(void);
243    void (*scr_prerendersync)(void);
244    bool scr_is_ogl;
245 
246    unsigned int snd_enabled;
247    unsigned int snd_playback_rate;
248    unsigned int snd_bits;
249    unsigned int snd_stereo;
250    unsigned int snd_volume;
251    unsigned int snd_pp_device;
252    unsigned int snd_buffersize;
253    unsigned char *snd_bufferptr;
254 
255    unsigned int (*video_monitor)(double r, double g, double b);
256 
257    union
258    {
259 #ifdef MSB_FIRST
260       struct
261       {
262          unsigned int high;
263          unsigned int low;
264       };
265 #else
266       struct
267       {
268          unsigned int low;
269          unsigned int high;
270       };
271 #endif
272       int64_t both;
273    } snd_cycle_count_init;
274 
275    unsigned int kbd_layout;
276 
277    unsigned int max_tracksize;
278    char snap_path[_MAX_PATH + 1];
279    char snap_file[_MAX_PATH + 1];
280    bool snap_zip;
281    char drvA_path[_MAX_PATH + 1];
282    char drvA_file[_MAX_PATH + 1];
283    bool drvA_zip;
284    unsigned int drvA_format;
285    char drvB_path[_MAX_PATH + 1];
286    char drvB_file[_MAX_PATH + 1];
287    bool drvB_zip;
288    unsigned int drvB_format;
289    char tape_path[_MAX_PATH + 1];
290    char tape_file[_MAX_PATH + 1];
291    bool tape_zip;
292    char printer_file[_MAX_PATH + 1];
293    char sdump_file[_MAX_PATH + 1];
294 
295    char rom_path[_MAX_PATH + 1];
296    char rom_file[16][_MAX_PATH + 1];
297    char rom_mf2[_MAX_PATH + 1];
298 } t_CPC;
299 
300 typedef struct {
301    unsigned int requested_addr;
302    unsigned int next_addr;
303    unsigned int addr;
304    unsigned int next_address;
305    unsigned int scr_base;
306    unsigned int char_count;
307    unsigned int line_count;
308    unsigned int raster_count;
309    unsigned int hsw;
310    unsigned int hsw_count;
311    unsigned int vsw;
312    unsigned int vsw_count;
313    unsigned int flag_hadhsync;
314    unsigned int flag_inmonhsync;
315    unsigned int flag_invsync;
316    unsigned int flag_invta;
317    unsigned int flag_newscan;
318    unsigned int flag_reschar;
319    unsigned int flag_resframe;
320    unsigned int flag_resnext;
321    unsigned int flag_resscan;
322    unsigned int flag_resvsync;
323    unsigned int flag_startvta;
324    unsigned int last_hend;
325    unsigned int reg5;
326    unsigned int r7match;
327    unsigned int r9match;
328    unsigned int hstart;
329    unsigned int hend;
330    void (*CharInstMR)(void);
331    void (*CharInstSL)(void);
332    unsigned char reg_select;
333    unsigned char registers[18];
334 
335    // 6128+ split screen support
336    unsigned int split_addr;
337    unsigned char split_sl;
338    unsigned int sl_count;
339    unsigned char interrupt_sl;
340 } t_CRTC;
341 
342 typedef struct {
343    int timeout;
344    int motor;
345    int led;
346    int flags;
347    int phase;
348    int byte_count;
349    int buffer_count;
350    int cmd_length;
351    int res_length;
352    int cmd_direction;
353    void (*cmd_handler)(void);
354    unsigned char *buffer_ptr;
355    unsigned char *buffer_endptr;
356    unsigned char command[12];
357    unsigned char result[8];
358 } t_FDC;
359 
360 typedef struct {
361 	unsigned int hs_count;
362    unsigned char ROM_config;
363    unsigned char lower_ROM_bank;
364    bool registerPageOn;
365    unsigned char RAM_bank;
366    unsigned char RAM_config;
367    unsigned char upper_ROM;
368    unsigned int requested_scr_mode;
369    unsigned int scr_mode;
370    unsigned char pen;
371    unsigned char ink_values[17];
372    unsigned int palette[34];
373    unsigned char sl_count;
374    unsigned char int_delay;
375 } t_GateArray;
376 
377 typedef struct {
378    unsigned char control;
379    unsigned char portA;
380    unsigned char portB;
381    unsigned char portC;
382 } t_PPI;
383 
384 typedef struct
385 {
386    union
387    {
388       struct
389       {
390          #ifdef MSB_FIRST
391          unsigned int high;
392          unsigned int low;
393          #else
394          unsigned int low;
395          unsigned int high;
396          #endif
397       };
398       int64_t both;
399    } cycle_count;
400 
401    unsigned int buffer_full;
402    unsigned char control;
403    unsigned char reg_select;
404    union {
405       unsigned char Index[16];
406       struct {
407          unsigned char TonALo, TonAHi;
408          unsigned char TonBLo, TonBHi;
409          unsigned char TonCLo, TonCHi;
410          unsigned char Noise;
411          unsigned char Mixer;
412          unsigned char AmplitudeA, AmplitudeB, AmplitudeC;
413          unsigned char EnvelopeLo, EnvelopeHi;
414          unsigned char EnvType;
415          unsigned char PortA;
416          unsigned char PortB;
417       };
418    } RegisterAY;
419    int AmplitudeEnv;
420    bool FirstPeriod;
421    void (*Synthesizer)(void);
422 } t_PSG;
423 
424 typedef struct {
425    int scrln;
426    int scanline;
427    unsigned int flag_drawing;
428    unsigned int frame_completed;
429 } t_VDU;
430 
431 typedef struct {
432    unsigned char CHRN[4]; // the CHRN for this sector
433    unsigned char flags[4]; // ST1 and ST2 - reflects any possible error conditions
434    unsigned char *data; // pointer to sector data
435    unsigned int size; // sector size in bytes
436 
437    // weak sector support
438    unsigned int weak_versions; // number of versions of this sector (should be 1 except for weak/random sectors)
439    unsigned int weak_read_version; // version of the sector to return when reading
440    unsigned int total_size; // total data size in bytes
441 } t_sector;
442 
443 typedef struct {
444    unsigned int sectors; // sector count for this track
445    unsigned int size; // track size in bytes
446    unsigned char *data; // pointer to track data
447    t_sector sector[DSK_SECTORMAX]; // array of sector information structures
448 } t_track;
449 
450 typedef struct {
451    unsigned int tracks; // total number of tracks
452    unsigned int current_track; // location of drive head
453    unsigned int sides; // total number of sides
454    unsigned int current_side; // side being accessed
455    unsigned int current_sector; // sector being accessed
456    unsigned int altered; // has the image been modified?
457    unsigned int write_protected; // is the image write protected?
458    unsigned int random_DEs; // sectors with Data Errors return random data?
459    unsigned int flipped; // reverse the side to access?
460    t_track track[DSK_TRACKMAX][DSK_SIDEMAX]; // array of track information structures
461 } t_drive;
462 
463 typedef struct {
464    char *pchZipFile;
465    char *pchExtension;
466    char *pchFileNames;
467    char *pchSelection;
468    int iFiles;
469    unsigned int dwOffset;
470 } t_zip_info;
471 
472 #define MAX_DISK_FORMAT 8
473 #define DEFAULT_DISK_FORMAT 0
474 #define FIRST_CUSTOM_DISK_FORMAT 2
475 typedef struct {
476    unsigned char label[40]; // label to display in options dialog
477    unsigned int tracks; // number of tracks
478    unsigned int sides; // number of sides
479    unsigned int sectors; // sectors per track
480    unsigned int sector_size; // sector size as N value
481    unsigned int gap3_length; // GAP#3 size
482    unsigned char filler_byte; // default byte to use
483    unsigned char sector_ids[2][16]; // sector IDs
484 } t_disk_format;
485 
486 
487 // cap32.cpp
488 void emulator_reset(bool bolMF2Reset);
489 int video_set_palette (void);
490 void video_set_palette_antialias (void);
491 int emulator_select_ROM (void);
492 size_t get_ram_size(void);
493 
494 // fdc.c
495 #ifdef __cplusplus
496 extern "C" {
497 #endif
498 
499 unsigned char* sector_get_read_data(t_sector * sector);
500 void sector_set_sizes(t_sector * sector, unsigned int size, unsigned int total_size);
501 
502 void fdc_write_data(unsigned char val);
503 unsigned char fdc_read_status(void);
504 uint8_t fdc_read_data(void);
505 
506 #ifdef __cplusplus
507 }
508 #endif
509 
510 // psg.c
511 void SetAYRegister(int Num, unsigned char Value);
512 void Calculate_Level_Tables(void);
513 void ResetAYChipEmulation(void);
514 void InitAYCounterVars(void);
515 void InitAY(void);
516 
517 #endif
518