1 /*
2  * viciitypes.h - A cycle-exact event-driven MOS6569 (VIC-II) emulation.
3  *
4  * Written by
5  *  Ettore Perazzoli <ettore@comm2000.it>
6  *  Andreas Boose <viceteam@t-online.de>
7  *  Daniel Kahlin <daniel@kahlin.net>
8  *
9  * This file is part of VICE, the Versatile Commodore Emulator.
10  * See README for copyright notice.
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25  *  02111-1307  USA.
26  *
27  */
28 
29 #ifndef VICE_VICIITYPES_H
30 #define VICE_VICIITYPES_H
31 
32 #include "raster.h"
33 #include "types.h"
34 
35 /* Screen constants.  */
36 #define VICII_SCREEN_XPIX                  320
37 #define VICII_SCREEN_YPIX                  200
38 #define VICII_SCREEN_TEXTCOLS              40
39 #define VICII_SCREEN_TEXTLINES             25
40 #define VICII_SCREEN_CHARHEIGHT            8
41 
42 #define VICII_NUM_SPRITES      8
43 #define VICII_NUM_COLORS       16
44 
45 /* This macro translated PAL cycles 1 to 63 into our internal
46    representation, i.e 0-63. */
47 #define VICII_PAL_CYCLE(c) ((c) - 1)
48 
49 /* Common parameters for all video standards */
50 #define VICII_25ROW_START_LINE    0x33
51 #define VICII_25ROW_STOP_LINE     0xfb
52 #define VICII_24ROW_START_LINE    0x37
53 #define VICII_24ROW_STOP_LINE     0xf7
54 
55 /* Bad line range.  */
56 #define VICII_FIRST_DMA_LINE      0x30
57 #define VICII_LAST_DMA_LINE       0xf7
58 
59 /* drawing constants. */
60 #define VICII_DRAW_BUFFER_SIZE (65 * 8)
61 
62 /* just a dummy for the vicii-draw.c wrapper */
63 #define VICII_DUMMY_MODE (0)
64 
65 
66 /* VIC-II structures.  This is meant to be used by VIC-II modules
67    *exclusively*!  */
68 
69 struct vicii_light_pen_s {
70     int state;
71     int triggered;
72     int x, y, x_extra_bits;
73     CLOCK trigger_cycle;
74 };
75 typedef struct vicii_light_pen_s vicii_light_pen_t;
76 
77 struct vicii_sprite_s {
78     /* Sprite data to display */
79     uint32_t data;
80     /* 6 bit counters */
81     uint8_t mc;
82     uint8_t mcbase;
83     /* 8 bit pointer */
84     uint8_t pointer;
85     /* Expansion flop */
86     int exp_flop;
87     /* X coordinate */
88     int x;
89 };
90 typedef struct vicii_sprite_s vicii_sprite_t;
91 
92 struct video_chip_cap_s;
93 
94 struct vicii_s {
95     /* Flag: Are we initialized?  */
96     int initialized;            /* = 0; */
97 
98     /* VIC-II raster.  */
99     raster_t raster;
100 
101     /* VIC-II registers.  */
102     uint8_t regs[0x40];
103 
104     /* Cycle # within the current line.  */
105     unsigned int raster_cycle;
106 
107     /* Cycle flags for the cycle table */
108     unsigned int cycle_flags;
109 
110     /* Current line.  */
111     unsigned int raster_line;
112 
113     /* Start of frame flag.  */
114     int start_of_frame;
115 
116     /* Interrupt register.  */
117     int irq_status;             /* = 0; */
118 
119     /* Line for raster compare IRQ.  */
120     unsigned int raster_irq_line;
121 
122     /* Flag for raster compare edge detect.  */
123     int raster_irq_triggered;
124 
125     /* Pointer to the base of RAM seen by the VIC-II.  */
126     /* address is base of 64k bank. vbank adds 0/16k/32k/48k to get actual
127        video address */
128     uint8_t *ram_base_phi1;                /* = VIC-II address during Phi1; */
129     uint8_t *ram_base_phi2;                /* = VIC-II address during Phi2; */
130 
131     /* valid VIC-II address bits for Phi1 and Phi2. After masking
132        the address, it is or'd with the offset value to set always-1 bits */
133     uint16_t vaddr_mask_phi1;            /* mask of valid address bits */
134     uint16_t vaddr_mask_phi2;            /* mask of valid address bits */
135     uint16_t vaddr_offset_phi1;          /* mask of address bits always set */
136     uint16_t vaddr_offset_phi2;          /* mask of address bits always set */
137 
138     /* Those two values determine where in the address space the chargen
139        ROM is mapped. Use mask=0x7000, value=0x1000 for the C64. */
140     uint16_t vaddr_chargen_mask_phi1;    /* address bits to comp. for chargen */
141     uint16_t vaddr_chargen_mask_phi2;    /* address bits to comp. for chargen */
142     uint16_t vaddr_chargen_value_phi1;   /* compare value for chargen */
143     uint16_t vaddr_chargen_value_phi2;   /* compare value for chargen */
144 
145     /* Screen memory buffers (chars and color).  */
146     uint8_t vbuf[VICII_SCREEN_TEXTCOLS];
147     uint8_t cbuf[VICII_SCREEN_TEXTCOLS];
148 
149     /* Graphics buffer (bitmap/LinearB) */
150     uint8_t gbuf;
151 
152     /* Current rendering position into the draw buffer */
153     int dbuf_offset;
154 
155     /* Draw buffer for a full line (one byte per pixel) */
156     uint8_t dbuf[VICII_DRAW_BUFFER_SIZE];
157 
158     /* parsed vicii register fields */
159     unsigned int ysmooth;
160 
161     /* If this flag is set, bad lines (DMA's) can happen.  */
162     int allow_bad_lines;
163 
164     /* Sprite-sprite and sprite-background collision registers.  */
165     uint8_t sprite_sprite_collisions;
166     uint8_t sprite_background_collisions;
167 
168     /* flag to signal collision clearing */
169     uint8_t clear_collisions;
170 
171     /* Flag: are we in idle state? */
172     int idle_state;
173 
174     /* Internal memory pointer (VCBASE).  */
175     int vcbase;
176 
177     /* Internal memory counter (VC).  */
178     int vc;
179 
180     /* Internal row counter (RC).  */
181     int rc;
182 
183     /* Offset to the vbuf/cbuf buffer (VMLI) */
184     int vmli;
185 
186     /* Flag: is the current line a `bad' line? */
187     int bad_line;
188 
189     /* Light pen.  */
190     vicii_light_pen_t light_pen;
191 
192     /* Start of the memory bank seen by the VIC-II.  */
193     int vbank_phi1;                     /* = 0; */
194     int vbank_phi2;                     /* = 0; */
195 
196     /* All the VIC-II logging goes here.  */
197     signed int log;
198 
199     /* Delayed mode selection */
200     uint8_t reg11_delay;
201 
202     /* Fetch state */
203     int prefetch_cycles;
204 
205     /* Mask for sprites being displayed.  */
206     unsigned int sprite_display_bits;
207 
208     /* Flag: is sprite DMA active? */
209     uint8_t sprite_dma;
210 
211     /* State of sprites. */
212     vicii_sprite_t sprite[VICII_NUM_SPRITES];
213 
214     /* Geometry and timing parameters of the selected VIC-II emulation.  */
215     unsigned int screen_height;
216     int first_displayed_line;
217     int last_displayed_line;
218 
219     int screen_leftborderwidth;
220     int screen_rightborderwidth;
221 
222     /* parameters (set by vicii-chip-model). */
223     int cycles_per_line;
224     int color_latency;
225     int lightpen_old_irq_mode;
226 
227     /* cycle table (set by vicii-chip-model). */
228     unsigned int cycle_table[65];
229 
230     /* last color register update (set by vicii-mem.c,
231        cleared by vicii-draw-cycle.c */
232     uint8_t last_color_reg;
233     uint8_t last_color_value;
234 
235     /* Last value read by VICII during phi1.  */
236     uint8_t last_read_phi1;
237 
238     /* Last value on the internal VICII bus during phi2.  */
239     uint8_t last_bus_phi2;
240 
241     /* Vertical border flag */
242     int vborder;
243 
244     /* latched set of Vertical border flag */
245     int set_vborder;
246 
247     /* Main border flag (this is what controls rendering) */
248     int main_border;
249 
250     /* Counter used for DRAM refresh accesses.  */
251     uint8_t refresh_counter;
252 
253     /* Video chip capabilities.  */
254     struct video_chip_cap_s *video_chip_cap;
255 
256     unsigned int int_num;
257 };
258 typedef struct vicii_s vicii_t;
259 
260 extern vicii_t vicii;
261 
262 /* Private function calls, used by the other VIC-II modules.  */
263 extern void vicii_raster_draw_handler(void);
264 
265 /* Debugging options.  */
266 
267 /* #define VICII_VMODE_DEBUG */
268 /* #define VICII_RASTER_DEBUG */
269 /* #define VICII_REGISTERS_DEBUG */
270 /* #define VICII_CYCLE_DEBUG */
271 
272 #ifdef VICII_VMODE_DEBUG
273 #define VICII_DEBUG_VMODE(x) log_debug x
274 #else
275 #define VICII_DEBUG_VMODE(x)
276 #endif
277 
278 #ifdef VICII_RASTER_DEBUG
279 #define VICII_DEBUG_RASTER(x) log_debug x
280 #else
281 #define VICII_DEBUG_RASTER(x)
282 #endif
283 
284 #ifdef VICII_REGISTERS_DEBUG
285 #define VICII_DEBUG_REGISTER(x) log_debug x
286 #else
287 #define VICII_DEBUG_REGISTER(x)
288 #endif
289 
290 #ifdef VICII_CYCLE_DEBUG
291 #define VICII_DEBUG_CYCLE(x) log_debug x
292 #else
293 #define VICII_DEBUG_CYCLE(x)
294 #endif
295 
296 #endif
297