]> Shamusworld >> Repos - virtualjaguar/blob - src/tom.cpp
Incremental changes to get Jaguar engine to work in QT environment.
[virtualjaguar] / src / tom.cpp
1 //
2 // TOM Processing
3 //
4 // Originally by David Raingeard (cal2)
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups and endian wrongness amelioration by James L. Hammons
7 // (C) 2010 Underground Software
8 //
9 // JLH = James L. Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -------------------------------------------------------------
13 // JLH  01/16/2010  Created this log ;-)
14 //
15 // Note: Endian wrongness probably stems from the MAME origins of this emu and
16 //       the braindead way in which MAME handles memory. :-)
17 //
18 // Note: TOM has only a 16K memory space
19 //
20 //      ------------------------------------------------------------
21 //      TOM REGISTERS (Mapped by Aaron Giles)
22 //      ------------------------------------------------------------
23 //      F00000-F0FFFF   R/W   xxxxxxxx xxxxxxxx   Internal Registers
24 //      F00000          R/W   -x-xx--- xxxxxxxx   MEMCON1 - memory config reg 1
25 //                            -x------ --------      (CPU32 - is the CPU 32bits?)
26 //                            ---xx--- --------      (IOSPEED - external I/O clock cycles)
27 //                            -------- x-------      (FASTROM - reduces ROM clock cycles)
28 //                            -------- -xx-----      (DRAMSPEED - sets RAM clock cycles)
29 //                            -------- ---xx---      (ROMSPEED - sets ROM clock cycles)
30 //                            -------- -----xx-      (ROMWIDTH - sets width of ROM: 8,16,32,64 bits)
31 //                            -------- -------x      (ROMHI - controls ROM mapping)
32 //      F00002          R/W   --xxxxxx xxxxxxxx   MEMCON2 - memory config reg 2
33 //                            --x----- --------      (HILO - image display bit order)
34 //                            ---x---- --------      (BIGEND - big endian addressing?)
35 //                            ----xxxx --------      (REFRATE - DRAM refresh rate)
36 //                            -------- xx------      (DWIDTH1 - DRAM1 width: 8,16,32,64 bits)
37 //                            -------- --xx----      (COLS1 - DRAM1 columns: 256,512,1024,2048)
38 //                            -------- ----xx--      (DWIDTH0 - DRAM0 width: 8,16,32,64 bits)
39 //                            -------- ------xx      (COLS0 - DRAM0 columns: 256,512,1024,2048)
40 //      F00004          R/W   -----xxx xxxxxxxx   HC - horizontal count
41 //                            -----x-- --------      (which half of the display)
42 //                            ------xx xxxxxxxx      (10-bit counter)
43 //      F00006          R/W   ----xxxx xxxxxxxx   VC - vertical count
44 //                            ----x--- --------      (which field is being generated)
45 //                            -----xxx xxxxxxxx      (11-bit counter)
46 //      F00008          R     -----xxx xxxxxxxx   LPH - light pen horizontal position
47 //      F0000A          R     -----xxx xxxxxxxx   LPV - light pen vertical position
48 //      F00010-F00017   R     xxxxxxxx xxxxxxxx   OB - current object code from the graphics processor
49 //      F00020-F00023     W   xxxxxxxx xxxxxxxx   OLP - start of the object list
50 //      F00026            W   -------- -------x   OBF - object processor flag
51 //      F00028            W   ----xxxx xxxxxxxx   VMODE - video mode
52 //                        W   ----xxx- --------      (PWIDTH1-8 - width of pixel in video clock cycles)
53 //                        W   -------x --------      (VARMOD - enable variable color resolution)
54 //                        W   -------- x-------      (BGEN - clear line buffer to BG color)
55 //                        W   -------- -x------      (CSYNC - enable composite sync on VSYNC)
56 //                        W   -------- --x-----      (BINC - local border color if INCEN)
57 //                        W   -------- ---x----      (INCEN - encrustation enable)
58 //                        W   -------- ----x---      (GENLOCK - enable genlock)
59 //                        W   -------- -----xx-      (MODE - CRY16,RGB24,DIRECT16,RGB16)
60 //                        W   -------- -------x      (VIDEN - enables video)
61 //      F0002A            W   xxxxxxxx xxxxxxxx   BORD1 - border color (red/green)
62 //      F0002C            W   -------- xxxxxxxx   BORD2 - border color (blue)
63 //      F0002E            W   ------xx xxxxxxxx   HP - horizontal period
64 //      F00030            W   -----xxx xxxxxxxx   HBB - horizontal blanking begin
65 //      F00032            W   -----xxx xxxxxxxx   HBE - horizontal blanking end
66 //      F00034            W   -----xxx xxxxxxxx   HSYNC - horizontal sync
67 //      F00036            W   ------xx xxxxxxxx   HVS - horizontal vertical sync
68 //      F00038            W   -----xxx xxxxxxxx   HDB1 - horizontal display begin 1
69 //      F0003A            W   -----xxx xxxxxxxx   HDB2 - horizontal display begin 2
70 //      F0003C            W   -----xxx xxxxxxxx   HDE - horizontal display end
71 //      F0003E            W   -----xxx xxxxxxxx   VP - vertical period
72 //      F00040            W   -----xxx xxxxxxxx   VBB - vertical blanking begin
73 //      F00042            W   -----xxx xxxxxxxx   VBE - vertical blanking end
74 //      F00044            W   -----xxx xxxxxxxx   VS - vertical sync
75 //      F00046            W   -----xxx xxxxxxxx   VDB - vertical display begin
76 //      F00048            W   -----xxx xxxxxxxx   VDE - vertical display end
77 //      F0004A            W   -----xxx xxxxxxxx   VEB - vertical equalization begin
78 //      F0004C            W   -----xxx xxxxxxxx   VEE - vertical equalization end
79 //      F0004E            W   -----xxx xxxxxxxx   VI - vertical interrupt
80 //      F00050            W   xxxxxxxx xxxxxxxx   PIT0 - programmable interrupt timer 0
81 //      F00052            W   xxxxxxxx xxxxxxxx   PIT1 - programmable interrupt timer 1
82 //      F00054            W   ------xx xxxxxxxx   HEQ - horizontal equalization end
83 //      F00058            W   xxxxxxxx xxxxxxxx   BG - background color
84 //      F000E0          R/W   ---xxxxx ---xxxxx   INT1 - CPU interrupt control register
85 //                            ---x---- --------      (C_JERCLR - clear pending Jerry ints)
86 //                            ----x--- --------      (C_PITCLR - clear pending PIT ints)
87 //                            -----x-- --------      (C_OPCLR - clear pending object processor ints)
88 //                            ------x- --------      (C_GPUCLR - clear pending graphics processor ints)
89 //                            -------x --------      (C_VIDCLR - clear pending video timebase ints)
90 //                            -------- ---x----      (C_JERENA - enable Jerry ints)
91 //                            -------- ----x---      (C_PITENA - enable PIT ints)
92 //                            -------- -----x--      (C_OPENA - enable object processor ints)
93 //                            -------- ------x-      (C_GPUENA - enable graphics processor ints)
94 //                            -------- -------x      (C_VIDENA - enable video timebase ints)
95 //      F000E2            W   -------- --------   INT2 - CPU interrupt resume register
96 //      F00400-F005FF   R/W   xxxxxxxx xxxxxxxx   CLUT - color lookup table A
97 //      F00600-F007FF   R/W   xxxxxxxx xxxxxxxx   CLUT - color lookup table B
98 //      F00800-F00D9F   R/W   xxxxxxxx xxxxxxxx   LBUF - line buffer A
99 //      F01000-F0159F   R/W   xxxxxxxx xxxxxxxx   LBUF - line buffer B
100 //      F01800-F01D9F   R/W   xxxxxxxx xxxxxxxx   LBUF - line buffer currently selected
101 //      ------------------------------------------------------------
102 //      F02000-F021FF   R/W   xxxxxxxx xxxxxxxx   GPU control registers
103 //      F02100          R/W   xxxxxxxx xxxxxxxx   G_FLAGS - GPU flags register
104 //                      R/W   x------- --------      (DMAEN - DMA enable)
105 //                      R/W   -x------ --------      (REGPAGE - register page)
106 //                        W   --x----- --------      (G_BLITCLR - clear blitter interrupt)
107 //                        W   ---x---- --------      (G_OPCLR - clear object processor int)
108 //                        W   ----x--- --------      (G_PITCLR - clear PIT interrupt)
109 //                        W   -----x-- --------      (G_JERCLR - clear Jerry interrupt)
110 //                        W   ------x- --------      (G_CPUCLR - clear CPU interrupt)
111 //                      R/W   -------x --------      (G_BLITENA - enable blitter interrupt)
112 //                      R/W   -------- x-------      (G_OPENA - enable object processor int)
113 //                      R/W   -------- -x------      (G_PITENA - enable PIT interrupt)
114 //                      R/W   -------- --x-----      (G_JERENA - enable Jerry interrupt)
115 //                      R/W   -------- ---x----      (G_CPUENA - enable CPU interrupt)
116 //                      R/W   -------- ----x---      (IMASK - interrupt mask)
117 //                      R/W   -------- -----x--      (NEGA_FLAG - ALU negative)
118 //                      R/W   -------- ------x-      (CARRY_FLAG - ALU carry)
119 //                      R/W   -------- -------x      (ZERO_FLAG - ALU zero)
120 //      F02104            W   -------- ----xxxx   G_MTXC - matrix control register
121 //                        W   -------- ----x---      (MATCOL - column/row major)
122 //                        W   -------- -----xxx      (MATRIX3-15 - matrix width)
123 //      F02108            W   ----xxxx xxxxxx--   G_MTXA - matrix address register
124 //      F0210C            W   -------- -----xxx   G_END - data organization register
125 //                        W   -------- -----x--      (BIG_INST - big endian instruction fetch)
126 //                        W   -------- ------x-      (BIG_PIX - big endian pixels)
127 //                        W   -------- -------x      (BIG_IO - big endian I/O)
128 //      F02110          R/W   xxxxxxxx xxxxxxxx   G_PC - GPU program counter
129 //      F02114          R/W   xxxxxxxx xx-xxxxx   G_CTRL - GPU control/status register
130 //                      R     xxxx---- --------      (VERSION - GPU version code)
131 //                      R/W   ----x--- --------      (BUS_HOG - hog the bus!)
132 //                      R/W   -----x-- --------      (G_BLITLAT - blitter interrupt latch)
133 //                      R/W   ------x- --------      (G_OPLAT - object processor int latch)
134 //                      R/W   -------x --------      (G_PITLAT - PIT interrupt latch)
135 //                      R/W   -------- x-------      (G_JERLAT - Jerry interrupt latch)
136 //                      R/W   -------- -x------      (G_CPULAT - CPU interrupt latch)
137 //                      R/W   -------- ---x----      (SINGLE_GO - single step one instruction)
138 //                      R/W   -------- ----x---      (SINGLE_STEP - single step mode)
139 //                      R/W   -------- -----x--      (FORCEINT0 - cause interrupt 0 on GPU)
140 //                      R/W   -------- ------x-      (CPUINT - send GPU interrupt to CPU)
141 //                      R/W   -------- -------x      (GPUGO - enable GPU execution)
142 //      F02118-F0211B   R/W   xxxxxxxx xxxxxxxx   G_HIDATA - high data register
143 //      F0211C-F0211F   R     xxxxxxxx xxxxxxxx   G_REMAIN - divide unit remainder
144 //      F0211C            W   -------- -------x   G_DIVCTRL - divide unit control
145 //                        W   -------- -------x      (DIV_OFFSET - 1=16.16 divide, 0=32-bit divide)
146 //      ------------------------------------------------------------
147 //      BLITTER REGISTERS
148 //      ------------------------------------------------------------
149 //      F02200-F022FF   R/W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   Blitter registers
150 //      F02200            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A1_BASE - A1 base register
151 //      F02204            W   -------- ---xxxxx -xxxxxxx xxxxx-xx   A1_FLAGS - A1 flags register
152 //                        W   -------- ---x---- -------- --------      (YSIGNSUB - invert sign of Y delta)
153 //                        W   -------- ----x--- -------- --------      (XSIGNSUB - invert sign of X delta)
154 //                        W   -------- -----x-- -------- --------      (Y add control)
155 //                        W   -------- ------xx -------- --------      (X add control)
156 //                        W   -------- -------- -xxxxxx- --------      (width in 6-bit floating point)
157 //                        W   -------- -------- -------x xx------      (ZOFFS1-6 - Z data offset)
158 //                        W   -------- -------- -------- --xxx---      (PIXEL - pixel size)
159 //                        W   -------- -------- -------- ------xx      (PITCH1-4 - data phrase pitch)
160 //      F02208            W   -xxxxxxx xxxxxxxx -xxxxxxx xxxxxxxx   A1_CLIP - A1 clipping size
161 //                        W   -xxxxxxx xxxxxxxx -------- --------      (height)
162 //                        W   -------- -------- -xxxxxxx xxxxxxxx      (width)
163 //      F0220C          R/W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A1_PIXEL - A1 pixel pointer
164 //                      R/W   xxxxxxxx xxxxxxxx -------- --------      (Y pixel value)
165 //                      R/W   -------- -------- xxxxxxxx xxxxxxxx      (X pixel value)
166 //      F02210            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A1_STEP - A1 step value
167 //                        W   xxxxxxxx xxxxxxxx -------- --------      (Y step value)
168 //                        W   -------- -------- xxxxxxxx xxxxxxxx      (X step value)
169 //      F02214            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A1_FSTEP - A1 step fraction value
170 //                        W   xxxxxxxx xxxxxxxx -------- --------      (Y step fraction value)
171 //                        W   -------- -------- xxxxxxxx xxxxxxxx      (X step fraction value)
172 //      F02218          R/W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A1_FPIXEL - A1 pixel pointer fraction
173 //                      R/W   xxxxxxxx xxxxxxxx -------- --------      (Y pixel fraction value)
174 //                      R/W   -------- -------- xxxxxxxx xxxxxxxx      (X pixel fraction value)
175 //      F0221C            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A1_INC - A1 increment
176 //                        W   xxxxxxxx xxxxxxxx -------- --------      (Y increment)
177 //                        W   -------- -------- xxxxxxxx xxxxxxxx      (X increment)
178 //      F02220            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A1_FINC - A1 increment fraction
179 //                        W   xxxxxxxx xxxxxxxx -------- --------      (Y increment fraction)
180 //                        W   -------- -------- xxxxxxxx xxxxxxxx      (X increment fraction)
181 //      F02224            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A2_BASE - A2 base register
182 //      F02228            W   -------- ---xxxxx -xxxxxxx xxxxx-xx   A2_FLAGS - A2 flags register
183 //                        W   -------- ---x---- -------- --------      (YSIGNSUB - invert sign of Y delta)
184 //                        W   -------- ----x--- -------- --------      (XSIGNSUB - invert sign of X delta)
185 //                        W   -------- -----x-- -------- --------      (Y add control)
186 //                        W   -------- ------xx -------- --------      (X add control)
187 //                        W   -------- -------- -xxxxxx- --------      (width in 6-bit floating point)
188 //                        W   -------- -------- -------x xx------      (ZOFFS1-6 - Z data offset)
189 //                        W   -------- -------- -------- --xxx---      (PIXEL - pixel size)
190 //                        W   -------- -------- -------- ------xx      (PITCH1-4 - data phrase pitch)
191 //      F0222C            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A2_MASK - A2 window mask
192 //      F02230          R/W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A2_PIXEL - A2 pixel pointer
193 //                      R/W   xxxxxxxx xxxxxxxx -------- --------      (Y pixel value)
194 //                      R/W   -------- -------- xxxxxxxx xxxxxxxx      (X pixel value)
195 //      F02234            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   A2_STEP - A2 step value
196 //                        W   xxxxxxxx xxxxxxxx -------- --------      (Y step value)
197 //                        W   -------- -------- xxxxxxxx xxxxxxxx      (X step value)
198 //      F02238            W   -xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_CMD - command register
199 //                        W   -x------ -------- -------- --------      (SRCSHADE - modify source intensity)
200 //                        W   --x----- -------- -------- --------      (BUSHI - hi priority bus)
201 //                        W   ---x---- -------- -------- --------      (BKGWREN - writeback destination)
202 //                        W   ----x--- -------- -------- --------      (DCOMPEN - write inhibit from data comparator)
203 //                        W   -----x-- -------- -------- --------      (BCOMPEN - write inhibit from bit coparator)
204 //                        W   ------x- -------- -------- --------      (CMPDST - compare dest instead of src)
205 //                        W   -------x xxx----- -------- --------      (logical operation)
206 //                        W   -------- ---xxx-- -------- --------      (ZMODE - Z comparator mode)
207 //                        W   -------- ------x- -------- --------      (ADDDSEL - select sum of src & dst)
208 //                        W   -------- -------x -------- --------      (PATDSEL - select pattern data)
209 //                        W   -------- -------- x------- --------      (TOPNEN - enable carry into top intensity nibble)
210 //                        W   -------- -------- -x------ --------      (TOPBEN - enable carry into top intensity byte)
211 //                        W   -------- -------- --x----- --------      (ZBUFF - enable Z updates in inner loop)
212 //                        W   -------- -------- ---x---- --------      (GOURD - enable gouraud shading in inner loop)
213 //                        W   -------- -------- ----x--- --------      (DSTA2 - reverses A2/A1 roles)
214 //                        W   -------- -------- -----x-- --------      (UPDA2 - add A2 step to A2 in outer loop)
215 //                        W   -------- -------- ------x- --------      (UPDA1 - add A1 step to A1 in outer loop)
216 //                        W   -------- -------- -------x --------      (UPDA1F - add A1 fraction step to A1 in outer loop)
217 //                        W   -------- -------- -------- x-------      (diagnostic use)
218 //                        W   -------- -------- -------- -x------      (CLIP_A1 - clip A1 to window)
219 //                        W   -------- -------- -------- --x-----      (DSTWRZ - enable dest Z write in inner loop)
220 //                        W   -------- -------- -------- ---x----      (DSTENZ - enable dest Z read in inner loop)
221 //                        W   -------- -------- -------- ----x---      (DSTEN - enables dest data read in inner loop)
222 //                        W   -------- -------- -------- -----x--      (SRCENX - enable extra src read at start of inner)
223 //                        W   -------- -------- -------- ------x-      (SRCENZ - enables source Z read in inner loop)
224 //                        W   -------- -------- -------- -------x      (SRCEN - enables source data read in inner loop)
225 //      F02238          R     xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_CMD - status register
226 //                      R     xxxxxxxx xxxxxxxx -------- --------      (inner count)
227 //                      R     -------- -------- xxxxxxxx xxxxxx--      (diagnostics)
228 //                      R     -------- -------- -------- ------x-      (STOPPED - when stopped in collision detect)
229 //                      R     -------- -------- -------- -------x      (IDLE - when idle)
230 //      F0223C            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_COUNT - counters register
231 //                        W   xxxxxxxx xxxxxxxx -------- --------      (outer loop count)
232 //                        W   -------- -------- xxxxxxxx xxxxxxxx      (inner loop count)
233 //      F02240-F02247     W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_SRCD - source data register
234 //      F02248-F0224F     W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_DSTD - destination data register
235 //      F02250-F02257     W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_DSTZ - destination Z register
236 //      F02258-F0225F     W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_SRCZ1 - source Z register 1
237 //      F02260-F02267     W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_SRCZ2 - source Z register 2
238 //      F02268-F0226F     W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_PATD - pattern data register
239 //      F02270            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_IINC - intensity increment
240 //      F02274            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_ZINC - Z increment
241 //      F02278            W   -------- -------- -------- -----xxx   B_STOP - collision control
242 //                        W   -------- -------- -------- -----x--      (STOPEN - enable blitter collision stops)
243 //                        W   -------- -------- -------- ------x-      (ABORT - abort after stop)
244 //                        W   -------- -------- -------- -------x      (RESUME - resume after stop)
245 //      F0227C            W   -------- xxxxxxxx xxxxxxxx xxxxxxxx   B_I3 - intensity 3
246 //      F02280            W   -------- xxxxxxxx xxxxxxxx xxxxxxxx   B_I2 - intensity 2
247 //      F02284            W   -------- xxxxxxxx xxxxxxxx xxxxxxxx   B_I1 - intensity 1
248 //      F02288            W   -------- xxxxxxxx xxxxxxxx xxxxxxxx   B_I0 - intensity 0
249 //      F0228C            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_Z3 - Z3
250 //      F02290            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_Z2 - Z2
251 //      F02294            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_Z1 - Z1
252 //      F02298            W   xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx   B_Z0 - Z0
253 //      ------------------------------------------------------------
254
255 #include "tom.h"
256
257 #include <string.h>                                                             // For memset()
258 #include <stdlib.h>                                                             // For rand()
259 #include "blitter.h"
260 #include "cry2rgb.h"
261 #include "event.h"
262 #include "gpu.h"
263 #include "jaguar.h"
264 #include "log.h"
265 #include "m68k.h"
266 //#include "memory.h"
267 #include "objectp.h"
268 #include "settings.h"
269 #include "video.h"
270
271 #define NEW_TIMER_SYSTEM
272
273 // TOM registers (offset from $F00000)
274
275 #define MEMCON1         0x00
276 #define MEMCON2         0x02
277 #define HC                      0x04
278 #define VC                      0x06
279 #define VMODE           0x28
280 #define   MODE          0x0006          // Line buffer to video generator mode
281 #define   BGEN          0x0080          // Background enable (CRY & RGB16 only)
282 #define   VARMOD        0x0100          // Mixed CRY/RGB16 mode (only works in MODE 0!)
283 #define   PWIDTH        0x0E00          // Pixel width in video clock cycles (value written + 1)
284 #define BORD1           0x2A            // Border green/red values (8 BPP)
285 #define BORD2           0x2C            // Border blue value (8 BPP)
286 #define HP                      0x2E            // Values range from 1 - 1024 (value written + 1)
287 #define HBB                     0x30
288 #define HBE                     0x32
289 #define HDB1            0x38            // Horizontal display begin 1
290 #define HDB2            0x3A
291 #define HDE                     0x3C
292 #define VP                      0x3E            // Value ranges from 1 - 2048 (value written + 1)
293 #define VBB                     0x40
294 #define VBE                     0x42
295 #define VS                      0x44
296 #define VDB                     0x46
297 #define VDE                     0x48
298 #define VI                      0x4E
299 #define BG                      0x58
300 #define INT1            0xE0
301
302 //NOTE: These arbitrary cutoffs are NOT taken into account for PAL jaguar screens. !!! FIX !!!
303
304 // Arbitrary video cutoff values (i.e., first/last visible spots on a TV, in HC ticks)
305 /*#define LEFT_VISIBLE_HC                       208
306 #define RIGHT_VISIBLE_HC                1528//*/
307 #define LEFT_VISIBLE_HC                 208
308 #define RIGHT_VISIBLE_HC                1488
309 //#define TOP_VISIBLE_VC                25
310 //#define BOTTOM_VISIBLE_VC             503
311 #define TOP_VISIBLE_VC                  31
312 #define BOTTOM_VISIBLE_VC               511
313
314 //Are these PAL horizontals correct?
315 //They seem to be for the most part, but there are some games that seem to be
316 //shifted over to the right from this "window".
317 #define LEFT_VISIBLE_HC_PAL             208
318 #define RIGHT_VISIBLE_HC_PAL    1488
319 #define TOP_VISIBLE_VC_PAL              67
320 #define BOTTOM_VISIBLE_VC_PAL   579
321
322 //This can be defined in the makefile as well...
323 //(It's easier to do it here, though...)
324 //#define TOM_DEBUG
325
326 uint8 tomRam8[0x4000];
327 uint32 tomWidth, tomHeight;
328 uint32 tomTimerPrescaler;
329 uint32 tomTimerDivider;
330 int32 tomTimerCounter;
331 //uint32 tom_scanline;
332 //uint32 hblankWidthInPixels = 0;
333 uint16 tom_jerry_int_pending, tom_timer_int_pending, tom_object_int_pending,
334         tom_gpu_int_pending, tom_video_int_pending;
335 //uint16 * tom_cry_rgb_mix_lut;
336 //int16 * TOMBackbuffer;
337 uint32 * TOMBackbuffer;
338
339 uint32 tomDeviceWidth;
340
341 static const char * videoMode_to_str[8] =
342         { "16 BPP CRY", "24 BPP RGB", "16 BPP DIRECT", "16 BPP RGB",
343           "Mixed mode", "24 BPP RGB", "16 BPP DIRECT", "16 BPP RGB" };
344
345 typedef void (render_xxx_scanline_fn)(uint32 *);
346
347 // Private function prototypes
348
349 void tom_render_16bpp_cry_scanline(uint32 * backbuffer);
350 void tom_render_24bpp_scanline(uint32 * backbuffer);
351 void tom_render_16bpp_direct_scanline(uint32 * backbuffer);
352 void tom_render_16bpp_rgb_scanline(uint32 * backbuffer);
353 void tom_render_16bpp_cry_rgb_mix_scanline(uint32 * backbuffer);
354
355 void tom_render_16bpp_cry_stretch_scanline(uint32 * backbuffer);
356 void tom_render_24bpp_stretch_scanline(uint32 * backbuffer);
357 void tom_render_16bpp_direct_stretch_scanline(uint32 * backbuffer);
358 void tom_render_16bpp_rgb_stretch_scanline(uint32 * backbuffer);
359 void tom_render_16bpp_cry_rgb_mix_stretch_scanline(uint32 * backbuffer);
360
361 render_xxx_scanline_fn * scanline_render_normal[] =
362 {
363         tom_render_16bpp_cry_scanline,
364         tom_render_24bpp_scanline,
365         tom_render_16bpp_direct_scanline,
366         tom_render_16bpp_rgb_scanline,
367         tom_render_16bpp_cry_rgb_mix_scanline,
368         tom_render_24bpp_scanline,
369         tom_render_16bpp_direct_scanline,
370         tom_render_16bpp_rgb_scanline
371 };
372
373 render_xxx_scanline_fn * scanline_render_stretch[] =
374 {
375         tom_render_16bpp_cry_stretch_scanline,
376         tom_render_24bpp_stretch_scanline,
377         tom_render_16bpp_direct_stretch_scanline,
378         tom_render_16bpp_rgb_stretch_scanline,
379         tom_render_16bpp_cry_rgb_mix_stretch_scanline,
380         tom_render_24bpp_stretch_scanline,
381         tom_render_16bpp_direct_stretch_scanline,
382         tom_render_16bpp_rgb_stretch_scanline,
383 };
384
385 render_xxx_scanline_fn * scanline_render[8];
386
387
388 // Screen info for various games [PAL]...
389 /*
390 BIOS
391 TOM: Horizontal Period written by M68K: 850 (+1*2 = 1702)
392 TOM: Horizontal Blank Begin written by M68K: 1711
393 TOM: Horizontal Blank End written by M68K: 158
394 TOM: Horizontal Display End written by M68K: 1696
395 TOM: Horizontal Display Begin 1 written by M68K: 166
396 TOM: Vertical Period written by M68K: 623 (non-interlaced)
397 TOM: Vertical Blank End written by M68K: 34
398 TOM: Vertical Display Begin written by M68K: 46
399 TOM: Vertical Display End written by M68K: 526
400 TOM: Vertical Blank Begin written by M68K: 600
401 TOM: Vertical Sync written by M68K: 618
402 TOM: Horizontal Display End written by M68K: 1665
403 TOM: Horizontal Display Begin 1 written by M68K: 203
404 TOM: Vertical Display Begin written by M68K: 38
405 TOM: Vertical Display End written by M68K: 518
406 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 151)
407 TOM: Horizontal Display End written by M68K: 1713
408 TOM: Horizontal Display Begin 1 written by M68K: 157
409 TOM: Vertical Display Begin written by M68K: 35
410 TOM: Vertical Display End written by M68K: 2047
411 Horizontal range: 157 - 1713 (width: 1557 / 4 = 389.25, / 5 = 315.4)
412
413 Asteroid
414 TOM: Horizontal Period written by M68K: 845 (+1*2 = 1692)
415 TOM: Horizontal Blank Begin written by M68K: 1700
416 TOM: Horizontal Blank End written by M68K: 122
417 TOM: Horizontal Display End written by M68K: 1600
418 TOM: Horizontal Display Begin 1 written by M68K: 268
419 TOM: Vertical Period written by M68K: 523 (non-interlaced)
420 TOM: Vertical Blank End written by M68K: 40
421 TOM: Vertical Display Begin written by M68K: 44
422 TOM: Vertical Display End written by M68K: 492
423 TOM: Vertical Blank Begin written by M68K: 532
424 TOM: Vertical Sync written by M68K: 513
425 TOM: Video Mode written by M68K: 04C7. PWIDTH = 3, MODE = 16 BPP RGB, flags: BGEN (VC = 461)
426
427 Rayman
428 TOM: Horizontal Display End written by M68K: 1713
429 TOM: Horizontal Display Begin 1 written by M68K: 157
430 TOM: Vertical Display Begin written by M68K: 35
431 TOM: Vertical Display End written by M68K: 2047
432 TOM: Video Mode written by M68K: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 89)
433 TOM: Horizontal Display Begin 1 written by M68K: 208
434 TOM: Horizontal Display End written by M68K: 1662
435 TOM: Vertical Display Begin written by M68K: 100
436 TOM: Vertical Display End written by M68K: 2047
437 TOM: Video Mode written by M68K: 07C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN VARMOD (VC = 205)
438 Horizontal range: 208 - 1662 (width: 1455 / 4 = 363.5)
439
440 Alien vs Predator
441 TOM: Vertical Display Begin written by M68K: 96
442 TOM: Vertical Display End written by M68K: 2047
443 TOM: Horizontal Display Begin 1 written by M68K: 239
444 TOM: Horizontal Display End written by M68K: 1692
445 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 378)
446 TOM: Vertical Display Begin written by M68K: 44
447 TOM: Vertical Display End written by M68K: 2047
448 TOM: Horizontal Display Begin 1 written by M68K: 239
449 TOM: Horizontal Display End written by M68K: 1692
450 TOM: Video Mode written by M68K: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 559)
451 TOM: Vertical Display Begin written by M68K: 84
452 TOM: Vertical Display End written by M68K: 2047
453 TOM: Horizontal Display Begin 1 written by M68K: 239
454 TOM: Horizontal Display End written by M68K: 1692
455 TOM: Vertical Display Begin written by M68K: 44
456 TOM: Vertical Display End written by M68K: 2047
457 TOM: Horizontal Display Begin 1 written by M68K: 239
458 TOM: Horizontal Display End written by M68K: 1692
459 Horizontal range: 239 - 1692 (width: 1454 / 4 = 363.5)
460
461 */
462
463 // Screen info for various games [NTSC]...
464 /*
465 Doom
466 TOM: Horizontal Display End written by M68K: 1727
467 TOM: Horizontal Display Begin 1 written by M68K: 123
468 TOM: Vertical Display Begin written by M68K: 25
469 TOM: Vertical Display End written by M68K: 2047
470 TOM: Video Mode written by M68K: 0EC1. PWIDTH = 8, MODE = 16 BPP CRY, flags: BGEN (VC = 5)
471 Also does PWIDTH = 4...
472 Vertical resolution: 238 lines
473
474 Rayman
475 TOM: Horizontal Display End written by M68K: 1727
476 TOM: Horizontal Display Begin 1 written by M68K: 123
477 TOM: Vertical Display Begin written by M68K: 25
478 TOM: Vertical Display End written by M68K: 2047
479 TOM: Vertical Interrupt written by M68K: 507
480 TOM: Video Mode written by M68K: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 92)
481 TOM: Horizontal Display Begin 1 written by M68K: 208
482 TOM: Horizontal Display End written by M68K: 1670
483 Display starts at 31, then 52!
484 Vertical resolution: 238 lines
485
486 Atari Karts
487 TOM: Horizontal Display End written by M68K: 1727
488 TOM: Horizontal Display Begin 1 written by M68K: 123
489 TOM: Vertical Display Begin written by M68K: 25
490 TOM: Vertical Display End written by M68K: 2047
491 TOM: Video Mode written by GPU: 08C7. PWIDTH = 5, MODE = 16 BPP RGB, flags: BGEN (VC = 4)
492 TOM: Video Mode written by GPU: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 508)
493 Display starts at 31 (PWIDTH = 4), 24 (PWIDTH = 5)
494
495 Iron Soldier
496 TOM: Vertical Interrupt written by M68K: 2047
497 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 0)
498 TOM: Horizontal Display End written by M68K: 1727
499 TOM: Horizontal Display Begin 1 written by M68K: 123
500 TOM: Vertical Display Begin written by M68K: 25
501 TOM: Vertical Display End written by M68K: 2047
502 TOM: Vertical Interrupt written by M68K: 507
503 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 369)
504 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 510)
505 TOM: Video Mode written by M68K: 06C3. PWIDTH = 4, MODE = 24 BPP RGB, flags: BGEN (VC = 510)
506 Display starts at 31
507 Vertical resolution: 238 lines
508 [Seems to be a problem between the horizontal positioning of the 16-bit CRY & 24-bit RGB]
509
510 JagMania
511 TOM: Horizontal Period written by M68K: 844 (+1*2 = 1690)
512 TOM: Horizontal Blank Begin written by M68K: 1713
513 TOM: Horizontal Blank End written by M68K: 125
514 TOM: Horizontal Display End written by M68K: 1696
515 TOM: Horizontal Display Begin 1 written by M68K: 166
516 TOM: Vertical Period written by M68K: 523 (non-interlaced)
517 TOM: Vertical Blank End written by M68K: 24
518 TOM: Vertical Display Begin written by M68K: 46
519 TOM: Vertical Display End written by M68K: 496
520 TOM: Vertical Blank Begin written by M68K: 500
521 TOM: Vertical Sync written by M68K: 517
522 TOM: Vertical Interrupt written by M68K: 497
523 TOM: Video Mode written by M68K: 04C1. PWIDTH = 3, MODE = 16 BPP CRY, flags: BGEN (VC = 270)
524 Display starts at 55
525
526 Double Dragon V
527 TOM: Horizontal Display End written by M68K: 1727
528 TOM: Horizontal Display Begin 1 written by M68K: 123
529 TOM: Vertical Display Begin written by M68K: 25
530 TOM: Vertical Display End written by M68K: 2047
531 TOM: Vertical Interrupt written by M68K: 507
532 TOM: Video Mode written by M68K: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 9)
533
534 Dino Dudes
535 TOM: Horizontal Display End written by M68K: 1823
536 TOM: Horizontal Display Begin 1 written by M68K: 45
537 TOM: Vertical Display Begin written by M68K: 40
538 TOM: Vertical Display End written by M68K: 2047
539 TOM: Vertical Interrupt written by M68K: 491
540 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 398)
541 Display starts at 11 (123 - 45 = 78, 78 / 4 = 19 pixels to skip)
542 Width is 417, so maybe width of 379 would be good (starting at 123, ending at 1639)
543 Vertical resolution: 238 lines
544
545 Flashback
546 TOM: Horizontal Display End written by M68K: 1727
547 TOM: Horizontal Display Begin 1 written by M68K: 188
548 TOM: Vertical Display Begin written by M68K: 1
549 TOM: Vertical Display End written by M68K: 2047
550 TOM: Vertical Interrupt written by M68K: 483
551 TOM: Video Mode written by M68K: 08C7. PWIDTH = 5, MODE = 16 BPP RGB, flags: BGEN (VC = 99)
552 Width would be 303 with above scheme, but border width would be 13 pixels
553
554 Trevor McFur
555 Vertical resolution: 238 lines
556 */
557
558 uint32 RGB16ToRGB32[0x10000];
559 uint32 CRY16ToRGB32[0x10000];
560 uint32 MIX16ToRGB32[0x10000];
561
562 #warning "This is not endian-safe. !!! FIX !!!"
563 void TOMFillLookupTables(void)
564 {
565         for(uint32 i=0; i<0x10000; i++)
566                 RGB16ToRGB32[i] = 0xFF000000
567                         | ((i & 0xF100) >> 8)  | ((i & 0xE000) >> 13)
568                         | ((i & 0x07C0) << 13) | ((i & 0x0700) << 8)
569                         | ((i & 0x003F) << 10) | ((i & 0x0030) << 4);
570
571
572         for(uint32 i=0; i<0x10000; i++)
573         {
574                 uint32 chrm = (i & 0xF000) >> 12,
575                         chrl = (i & 0x0F00) >> 8,
576                         y = (i & 0x00FF);
577
578                 uint32 r = (((uint32)redcv[chrm][chrl]) * y) >> 8,
579                         g = (((uint32)greencv[chrm][chrl]) * y) >> 8,
580                         b = (((uint32)bluecv[chrm][chrl]) * y) >> 8;
581
582                 CRY16ToRGB32[i] = 0xFF000000 | (b << 16) | (g << 8) | r;
583                 MIX16ToRGB32[i] = (i & 0x01 ? RGB16ToRGB32[i] : CRY16ToRGB32[i]);
584         }
585
586 //      for(uint32 i=0; i<0x10000; i++)
587 //              if (i & 0x01)
588 //                      MIX16ToRGB32[i] = RGB16ToRGB32[i];
589 }
590
591 void TOMSetPendingJERRYInt(void)
592 {
593         tom_jerry_int_pending = 1;
594 }
595
596 void TOMSetPendingTimerInt(void)
597 {
598         tom_timer_int_pending = 1;
599 }
600
601 void TOMSetPendingObjectInt(void)
602 {
603         tom_object_int_pending = 1;
604 }
605
606 void TOMSetPendingGPUInt(void)
607 {
608         tom_gpu_int_pending = 1;
609 }
610
611 void TOMSetPendingVideoInt(void)
612 {
613         tom_video_int_pending = 1;
614 }
615
616 uint8 * TOMGetRamPointer(void)
617 {
618         return tomRam8;
619 }
620
621 uint8 TOMGetVideoMode(void)
622 {
623         uint16 vmode = GET16(tomRam8, VMODE);
624         return ((vmode & VARMOD) >> 6) | ((vmode & MODE) >> 1);
625 }
626
627 //Used in only one place (and for debug purposes): OBJECTP.CPP
628 #warning "Used in only one place (and for debug purposes): OBJECTP.CPP !!! FIX !!!"
629 uint16 TOMGetVDB(void)
630 {
631         return GET16(tomRam8, VDB);
632 }
633
634 //
635 // 16 BPP CRY/RGB mixed mode rendering
636 //
637 void tom_render_16bpp_cry_rgb_mix_scanline(uint32 * backbuffer)
638 {
639 //CHANGED TO 32BPP RENDERING
640         uint16 width = tomWidth;
641         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
642
643         //New stuff--restrict our drawing...
644         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
645         //NOTE: May have to check HDB2 as well!
646         // Get start position in HC ticks
647         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL);
648         startPos /= pwidth;
649         if (startPos < 0)
650                 current_line_buffer += 2 * -startPos;
651         else
652 //This case doesn't properly handle the "start on the right side of virtual screen" case
653 //Dunno why--looks Ok...
654 //What *is* for sure wrong is that it doesn't copy the linebuffer's BG pixels...
655 //This should likely be 4 instead of 2 (?--not sure)
656                 backbuffer += 2 * startPos, width -= startPos;
657
658         while (width)
659         {
660                 uint16 color = (*current_line_buffer++) << 8;
661                 color |= *current_line_buffer++;
662                 *backbuffer++ = MIX16ToRGB32[color];
663                 width--;
664         }
665 }
666
667 //
668 // 16 BPP CRY mode rendering
669 //
670 void tom_render_16bpp_cry_scanline(uint32 * backbuffer)
671 {
672 //CHANGED TO 32BPP RENDERING
673         uint16 width = tomWidth;
674         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
675
676         //New stuff--restrict our drawing...
677         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
678         //NOTE: May have to check HDB2 as well!
679         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL);// Get start position in HC ticks
680         startPos /= pwidth;
681         if (startPos < 0)
682                 current_line_buffer += 2 * -startPos;
683         else
684 //This should likely be 4 instead of 2 (?--not sure)
685                 backbuffer += 2 * startPos, width -= startPos;
686
687         while (width)
688         {
689                 uint16 color = (*current_line_buffer++) << 8;
690                 color |= *current_line_buffer++;
691                 *backbuffer++ = CRY16ToRGB32[color];
692                 width--;
693         }
694 }
695
696 //
697 // 24 BPP mode rendering
698 //
699 void tom_render_24bpp_scanline(uint32 * backbuffer)
700 {
701 //CHANGED TO 32BPP RENDERING
702         uint16 width = tomWidth;
703         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
704
705         //New stuff--restrict our drawing...
706         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
707         //NOTE: May have to check HDB2 as well!
708         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL); // Get start position in HC ticks
709         startPos /= pwidth;
710         if (startPos < 0)
711                 current_line_buffer += 4 * -startPos;
712         else
713 //This should likely be 4 instead of 2 (?--not sure)
714                 backbuffer += 2 * startPos, width -= startPos;
715
716         while (width)
717         {
718                 uint32 g = *current_line_buffer++;
719                 uint32 r = *current_line_buffer++;
720                 current_line_buffer++;
721                 uint32 b = *current_line_buffer++;
722                 *backbuffer++ = 0xFF000000 | (b << 16) | (g << 8) | r;
723                 width--;
724         }
725 }
726
727 //Seems to me that this is NOT a valid mode--the JTRM seems to imply that you would need
728 //extra hardware outside of the Jaguar console to support this!
729 //
730 // 16 BPP direct mode rendering
731 //
732 void tom_render_16bpp_direct_scanline(uint32 * backbuffer)
733 {
734         uint16 width = tomWidth;
735         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
736
737         while (width)
738         {
739                 uint16 color = (*current_line_buffer++) << 8;
740                 color |= *current_line_buffer++;
741                 *backbuffer++ = color >> 1;
742                 width--;
743         }
744 }
745
746 //
747 // 16 BPP RGB mode rendering
748 //
749 void tom_render_16bpp_rgb_scanline(uint32 * backbuffer)
750 {
751 //CHANGED TO 32BPP RENDERING
752         // 16 BPP RGB: 0-5 green, 6-10 blue, 11-15 red
753
754         uint16 width = tomWidth;
755         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
756
757         //New stuff--restrict our drawing...
758         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
759         //NOTE: May have to check HDB2 as well!
760         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL); // Get start position in HC ticks
761         startPos /= pwidth;
762
763         if (startPos < 0)
764                 current_line_buffer += 2 * -startPos;
765         else
766 //This should likely be 4 instead of 2 (?--not sure)
767                 backbuffer += 2 * startPos, width -= startPos;
768
769         while (width)
770         {
771                 uint32 color = (*current_line_buffer++) << 8;
772                 color |= *current_line_buffer++;
773                 *backbuffer++ = RGB16ToRGB32[color];
774                 width--;
775         }
776 }
777
778 /////////////////////////////////////////////////////////////////////
779 // This stuff may just go away by itself, especially if we do some //
780 // good old OpenGL goodness...                                     //
781 /////////////////////////////////////////////////////////////////////
782
783 void tom_render_16bpp_cry_rgb_mix_stretch_scanline(uint32 *backbuffer)
784 {
785         uint16 width=tomWidth;
786         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
787
788         while (width)
789         {
790                 uint16 color = *current_line_buffer++;
791                 color <<= 8;
792                 color |= *current_line_buffer++;
793                 *backbuffer++ = MIX16ToRGB32[color];
794                 current_line_buffer += 2;
795                 width--;
796         }
797 }
798
799 void tom_render_16bpp_cry_stretch_scanline(uint32 *backbuffer)
800 {
801         uint32 chrm, chrl, y;
802
803         uint16 width=tomWidth;
804         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
805
806         while (width)
807         {
808                 uint16 color;
809                 color=*current_line_buffer++;
810                 color<<=8;
811                 color|=*current_line_buffer++;
812
813                 chrm = (color & 0xF000) >> 12;
814                 chrl = (color & 0x0F00) >> 8;
815                 y    = (color & 0x00FF);
816
817                 uint16 red   =  ((((uint32)redcv[chrm][chrl])*y)>>11);
818                 uint16 green =  ((((uint32)greencv[chrm][chrl])*y)>>11);
819                 uint16 blue  =  ((((uint32)bluecv[chrm][chrl])*y)>>11);
820
821                 uint16 color2;
822                 color2=*current_line_buffer++;
823                 color2<<=8;
824                 color2|=*current_line_buffer++;
825
826                 chrm = (color2 & 0xF000) >> 12;
827                 chrl = (color2 & 0x0F00) >> 8;
828                 y    = (color2 & 0x00FF);
829
830                 uint16 red2   = ((((uint32)redcv[chrm][chrl])*y)>>11);
831                 uint16 green2 = ((((uint32)greencv[chrm][chrl])*y)>>11);
832                 uint16 blue2  = ((((uint32)bluecv[chrm][chrl])*y)>>11);
833
834                 red=(red+red2)>>1;
835                 green=(green+green2)>>1;
836                 blue=(blue+blue2)>>1;
837
838                 *backbuffer++=(red<<10)|(green<<5)|blue;
839                 width--;
840         }
841 }
842
843 void tom_render_24bpp_stretch_scanline(uint32 *backbuffer)
844 {
845         uint16 width=tomWidth;
846         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
847
848         while (width)
849         {
850                 uint16 green=*current_line_buffer++;
851                 uint16 red=*current_line_buffer++;
852                 /*uint16 nc=*/current_line_buffer++;
853                 uint16 blue=*current_line_buffer++;
854                 red>>=3;
855                 green>>=3;
856                 blue>>=3;
857                 *backbuffer++=(red<<10)|(green<<5)|blue;
858                 current_line_buffer+=4;
859                 width--;
860         }
861 }
862
863 void tom_render_16bpp_direct_stretch_scanline(uint32 *backbuffer)
864 {
865         uint16 width=tomWidth;
866         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
867
868         while (width)
869         {
870                 uint16 color=*current_line_buffer++;
871                 color<<=8;
872                 color|=*current_line_buffer++;
873                 color>>=1;
874                 *backbuffer++=color;
875                 current_line_buffer+=2;
876                 width--;
877         }
878 }
879
880 void tom_render_16bpp_rgb_stretch_scanline(uint32 *backbuffer)
881 {
882         uint16 width=tomWidth;
883         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
884
885         while (width)
886         {
887                 uint16 color1=*current_line_buffer++;
888                 color1<<=8;
889                 color1|=*current_line_buffer++;
890                 color1>>=1;
891                 uint16 color2=*current_line_buffer++;
892                 color2<<=8;
893                 color2|=*current_line_buffer++;
894                 color2>>=1;
895                 uint16 red=(((color1&0x7c00)>>10)+((color2&0x7c00)>>10))>>1;
896                 uint16 green=(((color1&0x00003e0)>>5)+((color2&0x00003e0)>>5))>>1;
897                 uint16 blue=(((color1&0x0000001f))+((color2&0x0000001f)))>>1;
898
899                 color1=(red<<10)|(blue<<5)|green;
900                 *backbuffer++=color1;
901                 width--;
902         }
903 }
904
905 void TOMResetBackbuffer(uint32 * backbuffer)
906 {
907         TOMBackbuffer = backbuffer;
908 }
909
910 //
911 // Process a single scanline
912 //
913 void TOMExecScanline(uint16 scanline, bool render)
914 {
915         bool inActiveDisplayArea = true;
916
917 //Interlacing is still not handled correctly here... !!! FIX !!!
918         if (scanline & 0x01)                                                    // Execute OP only on even lines (non-interlaced only!)
919                 return;
920
921         if (scanline >= (uint16)GET16(tomRam8, VDB) && scanline < (uint16)GET16(tomRam8, VDE))
922         {
923                 if (render)
924                 {
925                         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
926                         uint8 bgHI = tomRam8[BG], bgLO = tomRam8[BG + 1];
927
928                         // Clear line buffer with BG
929                         if (GET16(tomRam8, VMODE) & BGEN) // && (CRY or RGB16)...
930                                 for(uint32 i=0; i<720; i++)
931                                         *current_line_buffer++ = bgHI, *current_line_buffer++ = bgLO;
932
933                         OPProcessList(scanline, render);
934                 }
935         }
936         else
937                 inActiveDisplayArea = false;
938
939         // Try to take PAL into account...
940
941         uint16 topVisible = (vjs.hardwareTypeNTSC ? TOP_VISIBLE_VC : TOP_VISIBLE_VC_PAL),
942                 bottomVisible = (vjs.hardwareTypeNTSC ? BOTTOM_VISIBLE_VC : BOTTOM_VISIBLE_VC_PAL);
943
944         // Here's our virtualized scanline code...
945
946         if (scanline >= topVisible && scanline < bottomVisible)
947         {
948                 if (inActiveDisplayArea)
949                 {
950 //NOTE: The following doesn't put BORDER color on the sides... !!! FIX !!!
951 #warning "The following doesn't put BORDER color on the sides... !!! FIX !!!"
952                         if (vjs.renderType == RT_NORMAL)
953                                 scanline_render[TOMGetVideoMode()](TOMBackbuffer);
954                         else//TV type render
955                         {
956 /*
957         tom_render_16bpp_cry_scanline,
958         tom_render_24bpp_scanline,
959         tom_render_16bpp_direct_scanline,
960         tom_render_16bpp_rgb_scanline,
961         tom_render_16bpp_cry_rgb_mix_scanline,
962         tom_render_24bpp_scanline,
963         tom_render_16bpp_direct_scanline,
964         tom_render_16bpp_rgb_scanline
965 #define VMODE           0x28
966 #define   MODE          0x0006          // Line buffer to video generator mode
967 #define   VARMOD        0x0100          // Mixed CRY/RGB16 mode (only works in MODE 0!)
968 */
969                                 uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
970                                 uint8 mode = ((GET16(tomRam8, VMODE) & MODE) >> 1);
971                                 bool varmod = GET16(tomRam8, VMODE) & VARMOD;
972 //The video texture line buffer ranges from 0 to 1279, with its left edge starting at
973 //LEFT_VISIBLE_HC. So, we need to start writing into the backbuffer at HDB1, using pwidth
974 //as our scaling factor. The way it generates its image on a real TV!
975
976 //So, for example, if HDB1 is less than LEFT_VISIBLE_HC, then we have to figure out where
977 //in the VTLB that we start writing pixels from the Jaguar line buffer (VTLB start=0,
978 //JLB=something).
979 #if 0
980 //
981 // 24 BPP mode rendering
982 //
983 void tom_render_24bpp_scanline(uint32 * backbuffer)
984 {
985 //CHANGED TO 32BPP RENDERING
986         uint16 width = tomWidth;
987         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
988
989         //New stuff--restrict our drawing...
990         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
991         //NOTE: May have to check HDB2 as well!
992         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL); // Get start position in HC ticks
993         startPos /= pwidth;
994         if (startPos < 0)
995                 current_line_buffer += 4 * -startPos;
996         else
997 //This should likely be 4 instead of 2 (?--not sure)
998                 backbuffer += 2 * startPos, width -= startPos;
999
1000         while (width)
1001         {
1002                 uint32 g = *current_line_buffer++;
1003                 uint32 r = *current_line_buffer++;
1004                 current_line_buffer++;
1005                 uint32 b = *current_line_buffer++;
1006                 *backbuffer++ = 0xFF000000 | (b << 16) | (g << 8) | r;
1007                 width--;
1008         }
1009 }
1010 #endif
1011
1012                         }
1013                 }
1014                 else
1015                 {
1016                         // If outside of VDB & VDE, then display the border color
1017                         uint32 * currentLineBuffer = TOMBackbuffer;
1018                         uint8 g = tomRam8[BORD1], r = tomRam8[BORD1 + 1], b = tomRam8[BORD2 + 1];
1019                         uint32 pixel = 0xFF000000 | (b << 16) | (g << 8) | r;
1020
1021                         for(uint32 i=0; i<tomWidth; i++)
1022                                 *currentLineBuffer++ = pixel;
1023                 }
1024
1025 #warning "!!! Need to move this to an interface file !!! FIX !!!"
1026 //              TOMBackbuffer += GetSDLScreenWidthInPixels();
1027                 TOMBackbuffer += tomDeviceWidth;
1028         }
1029 }
1030
1031 //
1032 // TOM initialization
1033 //
1034 void TOMInit(void)
1035 {
1036         OPInit();
1037         BlitterInit();
1038         TOMReset();
1039         // Setup the non-stretchy scanline rendering...
1040         memcpy(scanline_render, scanline_render_normal, sizeof(scanline_render));
1041         TOMFillLookupTables();
1042 }
1043
1044 void TOMDone(void)
1045 {
1046         OPDone();
1047         BlitterDone();
1048         WriteLog("TOM: Resolution %i x %i %s\n", TOMGetVideoModeWidth(), TOMGetVideoModeHeight(),
1049                 videoMode_to_str[TOMGetVideoMode()]);
1050 //      WriteLog("\ntom: object processor:\n");
1051 //      WriteLog("tom: pointer to object list: 0x%.8x\n",op_get_list_pointer());
1052 //      WriteLog("tom: INT1=0x%.2x%.2x\n",TOMReadByte(0xf000e0),TOMReadByte(0xf000e1));
1053 //      gpu_done();
1054 //      dsp_done();
1055 //      memory_free(tomRam8);
1056 //      memory_free(tom_cry_rgb_mix_lut);
1057 }
1058
1059 /*uint32 tom_getHBlankWidthInPixels(void)
1060 {
1061         return hblankWidthInPixels;
1062 }*/
1063
1064 uint32 TOMGetVideoModeWidth(void)
1065 {
1066         //These widths are pretty bogus. Should use HDB1/2 & HDE/HBB & PWIDTH to calc the width...
1067 //      uint32 width[8] = { 1330, 665, 443, 332, 266, 222, 190, 166 };
1068 //Temporary, for testing Doom...
1069 //      uint32 width[8] = { 1330, 665, 443, 332, 266, 222, 190, 332 };
1070
1071         // Note that the following PWIDTH values have the following pixel aspect ratios:
1072         // PWIDTH = 1 -> 0.25:1 (1:4) pixels (X:Y ratio)
1073         // PWIDTH = 2 -> 0.50:1 (1:2) pixels
1074         // PWIDTH = 3 -> 0.75:1 (3:4) pixels
1075         // PWIDTH = 4 -> 1.00:1 (1:1) pixels
1076         // PWIDTH = 5 -> 1.25:1 (5:4) pixels
1077         // PWIDTH = 6 -> 1.50:1 (3:2) pixels
1078         // PWIDTH = 7 -> 1.75:1 (7:4) pixels
1079         // PWIDTH = 8 -> 2.00:1 (2:1) pixels
1080
1081         // Also note that the JTRM says that PWIDTH of 4 gives pixels that are "about" square--
1082         // this implies that the other modes have pixels that are *not* square!
1083         // Also, I seriously doubt that you will see any games that use PWIDTH = 1!
1084
1085         // NOTE: Even though the PWIDTH value is + 1, here we're using a zero-based index and
1086         //       so we don't bother to add one...
1087 //      return width[(GET16(tomRam8, VMODE) & PWIDTH) >> 9];
1088
1089         // Now, we just calculate it...
1090 /*      uint16 hdb1 = GET16(tomRam8, HDB1), hde = GET16(tomRam8, HDE),
1091                 hbb = GET16(tomRam8, HBB), pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
1092 //      return ((hbb < hde ? hbb : hde) - hdb1) / pwidth;
1093 //Temporary, for testing Doom...
1094         return ((hbb < hde ? hbb : hde) - hdb1) / (pwidth == 8 ? 4 : pwidth);*/
1095
1096         // To make it easier to make a quasi-fixed display size, we restrict the viewing
1097         // area to an arbitrary range of the Horizontal Count.
1098         uint16 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
1099         return (vjs.hardwareTypeNTSC ? RIGHT_VISIBLE_HC - LEFT_VISIBLE_HC : RIGHT_VISIBLE_HC_PAL - LEFT_VISIBLE_HC_PAL) / pwidth;
1100 //Temporary, for testing Doom...
1101 //      return (RIGHT_VISIBLE_HC - LEFT_VISIBLE_HC) / (pwidth == 8 ? 4 : pwidth);
1102 ////    return (RIGHT_VISIBLE_HC - LEFT_VISIBLE_HC) / (pwidth == 4 ? 8 : pwidth);
1103
1104 // More speculating...
1105 // According to the JTRM, the number of potential pixels across is given by the
1106 // Horizontal Period (HP - in NTSC this is 845). The Horizontal Count counts from
1107 // zero to this value twice per scanline (the high bit is set on the second count).
1108 // HBE and HBB define the absolute "black" limits of the screen, while HDB1/2 and
1109 // HDE determine the extent of the OP "on" time. I.e., when the OP is turned on by
1110 // HDB1, it starts fetching the line from position 0 in LBUF.
1111
1112 // The trick, it would seem, is to figure out how long the typical visible scanline
1113 // of a TV is in HP ticks and limit the visible area to that (divided by PWIDTH, of
1114 // course). Using that length, we can establish an "absolute left display limit" with
1115 // which to measure HBB & HDB1/2 against when rendering LBUF (i.e., if HDB1 is 20 ticks
1116 // to the right of the ALDL and PWIDTH is 4, then start writing the LBUF starting at
1117 // backbuffer + 5 pixels).
1118
1119 // That's basically what we're doing now...!
1120 }
1121
1122 // *** SPECULATION ***
1123 // It might work better to virtualize the height settings, i.e., set the vertical
1124 // height at 240 lines and clip using the VDB and VDE/VP registers...
1125 // Same with the width... [Width is pretty much virtualized now.]
1126
1127 // Now that that the width is virtualized, let's virtualize the height. :-)
1128 uint32 TOMGetVideoModeHeight(void)
1129 {
1130 //      uint16 vmode = GET16(tomRam8, VMODE);
1131 //      uint16 vbe = GET16(tomRam8, VBE);
1132 //      uint16 vbb = GET16(tomRam8, VBB);
1133 //      uint16 vdb = GET16(tomRam8, VDB);
1134 //      uint16 vde = GET16(tomRam8, VDE);
1135 //      uint16 vp = GET16(tomRam8, VP);
1136
1137 /*      if (vde == 0xFFFF)
1138                 vde = vbb;//*/
1139
1140 //      return 227;//WAS:(vde/*-vdb*/) >> 1;
1141         // The video mode height probably works this way:
1142         // VC counts from 0 to VP. VDB starts the OP. Either when
1143         // VDE is reached or VP, the OP is stopped. Let's try it...
1144         // Also note that we're conveniently ignoring interlaced display modes...!
1145 //      return ((vde > vp ? vp : vde) - vdb) >> 1;
1146 //      return ((vde > vbb ? vbb : vde) - vdb) >> 1;
1147 //Let's try from the Vertical Blank interval...
1148 //Seems to work OK!
1149 //      return (vbb - vbe) >> 1;        // Again, doesn't take interlacing into account...
1150 // This of course doesn't take interlacing into account. But I haven't seen any
1151 // Jaguar software that takes advantage of it either...
1152 //Also, doesn't reflect PAL Jaguar either... !!! FIX !!! [DONE]
1153 //      return 240;                                                                             // Set virtual screen height to 240 lines...
1154         return (vjs.hardwareTypeNTSC ? 240 : 256);
1155 }
1156
1157 //
1158 // TOM reset code
1159 // Now PAL friendly!
1160 //
1161 void TOMReset(void)
1162 {
1163         OPReset();
1164         BlitterReset();
1165         memset(tomRam8, 0x00, 0x4000);
1166
1167         if (vjs.hardwareTypeNTSC)
1168         {
1169                 SET16(tomRam8, MEMCON1, 0x1861);
1170                 SET16(tomRam8, MEMCON2, 0x35CC);
1171                 SET16(tomRam8, HP, 844);                                // Horizontal Period (1-based; HP=845)
1172                 SET16(tomRam8, HBB, 1713);                      // Horizontal Blank Begin
1173                 SET16(tomRam8, HBE, 125);                               // Horizontal Blank End
1174                 SET16(tomRam8, HDE, 1665);                      // Horizontal Display End
1175                 SET16(tomRam8, HDB1, 203);                      // Horizontal Display Begin 1
1176                 SET16(tomRam8, VP, 523);                                // Vertical Period (1-based; in this case VP = 524)
1177                 SET16(tomRam8, VBE, 24);                                // Vertical Blank End
1178                 SET16(tomRam8, VDB, 38);                                // Vertical Display Begin
1179                 SET16(tomRam8, VDE, 518);                               // Vertical Display End
1180                 SET16(tomRam8, VBB, 500);                               // Vertical Blank Begin
1181                 SET16(tomRam8, VS, 517);                                // Vertical Sync
1182                 SET16(tomRam8, VMODE, 0x06C1);
1183         }
1184         else    // PAL Jaguar
1185         {
1186                 SET16(tomRam8, MEMCON1, 0x1861);
1187                 SET16(tomRam8, MEMCON2, 0x35CC);
1188                 SET16(tomRam8, HP, 850);                                // Horizontal Period
1189                 SET16(tomRam8, HBB, 1711);                      // Horizontal Blank Begin
1190                 SET16(tomRam8, HBE, 158);                               // Horizontal Blank End
1191                 SET16(tomRam8, HDE, 1665);                      // Horizontal Display End
1192                 SET16(tomRam8, HDB1, 203);                      // Horizontal Display Begin 1
1193                 SET16(tomRam8, VP, 623);                                // Vertical Period (1-based; in this case VP = 624)
1194                 SET16(tomRam8, VBE, 34);                                // Vertical Blank End
1195                 SET16(tomRam8, VDB, 38);                                // Vertical Display Begin
1196                 SET16(tomRam8, VDE, 518);                               // Vertical Display End
1197                 SET16(tomRam8, VBB, 600);                               // Vertical Blank Begin
1198                 SET16(tomRam8, VS, 618);                                // Vertical Sync
1199                 SET16(tomRam8, VMODE, 0x06C1);
1200         }
1201
1202         tomWidth = 0;
1203         tomHeight = 0;
1204
1205         tom_jerry_int_pending = 0;
1206         tom_timer_int_pending = 0;
1207         tom_object_int_pending = 0;
1208         tom_gpu_int_pending = 0;
1209         tom_video_int_pending = 0;
1210
1211         tomTimerPrescaler = 0;                                  // TOM PIT is disabled
1212         tomTimerDivider = 0;
1213         tomTimerCounter = 0;
1214         memcpy(scanline_render, scanline_render_normal, sizeof(scanline_render));
1215 }
1216
1217 //
1218 // TOM byte access (read)
1219 //
1220 uint8 TOMReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
1221 {
1222 //???Is this needed???
1223 // It seems so. Perhaps it's the +$8000 offset being written to (32-bit interface)?
1224 // However, the 32-bit interface is WRITE ONLY, so that can't be it...
1225 // Also, the 68K CANNOT make use of the 32-bit interface, since its bus width is only 16-bits...
1226 //      offset &= 0xFF3FFF;
1227
1228 #ifdef TOM_DEBUG
1229         WriteLog("TOM: Reading byte at %06X\n", offset);
1230 #endif
1231
1232         if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
1233                 return GPUReadByte(offset, who);
1234         else if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
1235                 return GPUReadByte(offset, who);
1236 /*      else if ((offset >= 0xF00010) && (offset < 0xF00028))
1237                 return OPReadByte(offset, who);*/
1238         else if ((offset >= 0xF02200) && (offset < 0xF022A0))
1239                 return BlitterReadByte(offset, who);
1240         else if (offset == 0xF00050)
1241                 return tomTimerPrescaler >> 8;
1242         else if (offset == 0xF00051)
1243                 return tomTimerPrescaler & 0xFF;
1244         else if (offset == 0xF00052)
1245                 return tomTimerDivider >> 8;
1246         else if (offset == 0xF00053)
1247                 return tomTimerDivider & 0xFF;
1248
1249         return tomRam8[offset & 0x3FFF];
1250 }
1251
1252 //
1253 // TOM word access (read)
1254 //
1255 uint16 TOMReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
1256 {
1257 //???Is this needed???
1258 //      offset &= 0xFF3FFF;
1259 #ifdef TOM_DEBUG
1260         WriteLog("TOM: Reading word at %06X\n", offset);
1261 #endif
1262 if (offset >= 0xF02000 && offset <= 0xF020FF)
1263         WriteLog("TOM: Read attempted from GPU register file by %s (unimplemented)!\n", whoName[who]);
1264
1265         if (offset == 0xF000E0)
1266         {
1267                 uint16 data = (tom_jerry_int_pending << 4) | (tom_timer_int_pending << 3)
1268                         | (tom_object_int_pending << 2) | (tom_gpu_int_pending << 1)
1269                         | (tom_video_int_pending << 0);
1270                 //WriteLog("tom: interrupt status is 0x%.4x \n",data);
1271                 return data;
1272         }
1273 //Shoud be handled by the jaguar main loop now... And it is! ;-)
1274 /*      else if (offset == 0xF00006)    // VC
1275         // What if we're in interlaced mode?
1276         // According to docs, in non-interlace mode VC is ALWAYS even...
1277 //              return (tom_scanline << 1);// + 1;
1278 //But it's causing Rayman to be fucked up... Why???
1279 //Because VC is even in NI mode when calling the OP! That's why!
1280                 return (tom_scanline << 1) + 1;//*/
1281 /*
1282 //      F00004          R/W   -----xxx xxxxxxxx   HC - horizontal count
1283 //                            -----x-- --------      (which half of the display)
1284 //                            ------xx xxxxxxxx      (10-bit counter)
1285 */
1286 // This is a kludge to get the HC working somewhat... What we really should do here
1287 // is check what the global time is at the time of the read and calculate the correct HC...
1288 // !!! FIX !!!
1289         else if (offset == 0xF00004)
1290                 return rand() & 0x03FF;
1291         else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE + 0x20))
1292                 return GPUReadWord(offset, who);
1293         else if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE + 0x1000))
1294                 return GPUReadWord(offset, who);
1295 /*      else if ((offset >= 0xF00010) && (offset < 0xF00028))
1296                 return OPReadWord(offset, who);*/
1297         else if ((offset >= 0xF02200) && (offset < 0xF022A0))
1298                 return BlitterReadWord(offset, who);
1299         else if (offset == 0xF00050)
1300                 return tomTimerPrescaler;
1301         else if (offset == 0xF00052)
1302                 return tomTimerDivider;
1303
1304         offset &= 0x3FFF;
1305         return (TOMReadByte(offset, who) << 8) | TOMReadByte(offset + 1, who);
1306 }
1307
1308 //
1309 // TOM byte access (write)
1310 //
1311 void TOMWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
1312 {
1313 //???Is this needed???
1314 // Perhaps on the writes--32-bit writes that is! And masked with FF7FFF...
1315         offset &= 0xFF3FFF;
1316
1317 #ifdef TOM_DEBUG
1318         WriteLog("TOM: Writing byte %02X at %06X\n", data, offset);
1319 #endif
1320
1321         if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
1322         {
1323                 GPUWriteByte(offset, data, who);
1324                 return;
1325         }
1326         else if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
1327         {
1328                 GPUWriteByte(offset, data, who);
1329                 return;
1330         }
1331 /*      else if ((offset >= 0xF00010) && (offset < 0xF00028))
1332         {
1333                 OPWriteByte(offset, data, who);
1334                 return;
1335         }*/
1336         else if ((offset >= 0xF02200) && (offset < 0xF022A0))
1337         {
1338                 BlitterWriteByte(offset, data, who);
1339                 return;
1340         }
1341         else if (offset == 0xF00050)
1342         {
1343                 tomTimerPrescaler = (tomTimerPrescaler & 0x00FF) | (data << 8);
1344                 TOMResetPIT();
1345                 return;
1346         }
1347         else if (offset == 0xF00051)
1348         {
1349                 tomTimerPrescaler = (tomTimerPrescaler & 0xFF00) | data;
1350                 TOMResetPIT();
1351                 return;
1352         }
1353         else if (offset == 0xF00052)
1354         {
1355                 tomTimerDivider = (tomTimerDivider & 0x00FF) | (data << 8);
1356                 TOMResetPIT();
1357                 return;
1358         }
1359         else if (offset == 0xF00053)
1360         {
1361                 tomTimerDivider = (tomTimerDivider & 0xFF00) | data;
1362                 TOMResetPIT();
1363                 return;
1364         }
1365         else if (offset >= 0xF00400 && offset <= 0xF007FF)      // CLUT (A & B)
1366         {
1367                 // Writing to one CLUT writes to the other
1368                 offset &= 0x5FF;                // Mask out $F00600 (restrict to $F00400-5FF)
1369                 tomRam8[offset] = data, tomRam8[offset + 0x200] = data;
1370         }
1371
1372         tomRam8[offset & 0x3FFF] = data;
1373 }
1374
1375 //
1376 // TOM word access (write)
1377 //
1378 void TOMWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
1379 {
1380 //???Is this needed???
1381         offset &= 0xFF3FFF;
1382
1383 #ifdef TOM_DEBUG
1384         WriteLog("TOM: Writing word %04X at %06X\n", data, offset);
1385 #endif
1386 if (offset == 0xF00000 + MEMCON1)
1387         WriteLog("TOM: Memory Configuration 1 written by %s: %04X\n", whoName[who], data);
1388 if (offset == 0xF00000 + MEMCON2)
1389         WriteLog("TOM: Memory Configuration 2 written by %s: %04X\n", whoName[who], data);
1390 if (offset >= 0xF02000 && offset <= 0xF020FF)
1391         WriteLog("TOM: Write attempted to GPU register file by %s (unimplemented)!\n", whoName[who]);
1392
1393         if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
1394         {
1395                 GPUWriteWord(offset, data, who);
1396                 return;
1397         }
1398         else if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
1399         {
1400                 GPUWriteWord(offset, data, who);
1401                 return;
1402         }
1403 //What's so special about this?
1404 /*      else if ((offset >= 0xF00000) && (offset < 0xF00002))
1405         {
1406                 TOMWriteByte(offset, data >> 8);
1407                 TOMWriteByte(offset+1, data & 0xFF);
1408         }*/
1409 /*      else if ((offset >= 0xF00010) && (offset < 0xF00028))
1410         {
1411                 OPWriteWord(offset, data, who);
1412                 return;
1413         }*/
1414         else if (offset == 0xF00050)
1415         {
1416                 tomTimerPrescaler = data;
1417                 TOMResetPIT();
1418                 return;
1419         }
1420         else if (offset == 0xF00052)
1421         {
1422                 tomTimerDivider = data;
1423                 TOMResetPIT();
1424                 return;
1425         }
1426         else if (offset == 0xF000E0)
1427         {
1428 //Check this out...
1429                 if (data & 0x0100)
1430                         tom_video_int_pending = 0;
1431                 if (data & 0x0200)
1432                         tom_gpu_int_pending = 0;
1433                 if (data & 0x0400)
1434                         tom_object_int_pending = 0;
1435                 if (data & 0x0800)
1436                         tom_timer_int_pending = 0;
1437                 if (data & 0x1000)
1438                         tom_jerry_int_pending = 0;
1439         }
1440         else if ((offset >= 0xF02200) && (offset <= 0xF0229F))
1441         {
1442                 BlitterWriteWord(offset, data, who);
1443                 return;
1444         }
1445         else if (offset >= 0xF00400 && offset <= 0xF007FE)      // CLUT (A & B)
1446         {
1447                 // Writing to one CLUT writes to the other
1448                 offset &= 0x5FF;                // Mask out $F00600 (restrict to $F00400-5FF)
1449 // Watch out for unaligned writes here! (Not fixed yet)
1450                 SET16(tomRam8, offset, data);
1451                 SET16(tomRam8, offset + 0x200, data);
1452         }
1453
1454         offset &= 0x3FFF;
1455         if (offset == 0x28)                     // VMODE (Why? Why not OBF?)
1456 //Actually, we should check to see if the Enable bit of VMODE is set before doing this... !!! FIX !!!
1457 #warning "Actually, we should check to see if the Enable bit of VMODE is set before doing this... !!! FIX !!!"
1458                 objectp_running = 1;
1459
1460         if (offset >= 0x30 && offset <= 0x4E)
1461                 data &= 0x07FF;                 // These are (mostly) 11-bit registers
1462         if (offset == 0x2E || offset == 0x36 || offset == 0x54)
1463                 data &= 0x03FF;                 // These are all 10-bit registers
1464
1465         TOMWriteByte(offset, data >> 8, who);
1466         TOMWriteByte(offset+1, data & 0xFF, who);
1467
1468 if (offset == VDB)
1469         WriteLog("TOM: Vertical Display Begin written by %s: %u\n", whoName[who], data);
1470 if (offset == VDE)
1471         WriteLog("TOM: Vertical Display End written by %s: %u\n", whoName[who], data);
1472 if (offset == VP)
1473         WriteLog("TOM: Vertical Period written by %s: %u (%sinterlaced)\n", whoName[who], data, (data & 0x01 ? "non-" : ""));
1474 if (offset == HDB1)
1475         WriteLog("TOM: Horizontal Display Begin 1 written by %s: %u\n", whoName[who], data);
1476 if (offset == HDE)
1477         WriteLog("TOM: Horizontal Display End written by %s: %u\n", whoName[who], data);
1478 if (offset == HP)
1479         WriteLog("TOM: Horizontal Period written by %s: %u (+1*2 = %u)\n", whoName[who], data, (data + 1) * 2);
1480 if (offset == VBB)
1481         WriteLog("TOM: Vertical Blank Begin written by %s: %u\n", whoName[who], data);
1482 if (offset == VBE)
1483         WriteLog("TOM: Vertical Blank End written by %s: %u\n", whoName[who], data);
1484 if (offset == VS)
1485         WriteLog("TOM: Vertical Sync written by %s: %u\n", whoName[who], data);
1486 if (offset == VI)
1487         WriteLog("TOM: Vertical Interrupt written by %s: %u\n", whoName[who], data);
1488 if (offset == HBB)
1489         WriteLog("TOM: Horizontal Blank Begin written by %s: %u\n", whoName[who], data);
1490 if (offset == HBE)
1491         WriteLog("TOM: Horizontal Blank End written by %s: %u\n", whoName[who], data);
1492 if (offset == VMODE)
1493         WriteLog("TOM: Video Mode written by %s: %04X. PWIDTH = %u, MODE = %s, flags:%s%s (VC = %u)\n", whoName[who], data, ((data >> 9) & 0x07) + 1, videoMode_to_str[(data & MODE) >> 1], (data & BGEN ? " BGEN" : ""), (data & VARMOD ? " VARMOD" : ""), GET16(tomRam8, VC));
1494
1495         // detect screen resolution changes
1496 //This may go away in the future, if we do the virtualized screen thing...
1497 //This may go away soon!
1498         if ((offset >= 0x28) && (offset <= 0x4F))
1499         {
1500                 uint32 width = TOMGetVideoModeWidth(), height = TOMGetVideoModeHeight();
1501
1502                 if ((width != tomWidth) || (height != tomHeight))
1503                 {
1504                         tomWidth = width, tomHeight = height;
1505
1506 #warning "!!! TOM: ResizeScreen commented out !!!"
1507 // No need to resize anything, since we're prepared for this...
1508 //                      if (vjs.renderType == RT_NORMAL)
1509 //                              ResizeScreen(tomWidth, tomHeight);
1510                 }
1511         }
1512 }
1513
1514 int TOMIRQEnabled(int irq)
1515 {
1516         // This is the correct byte in big endian... D'oh!
1517 //      return jaguar_byte_read(0xF000E1) & (1 << irq);
1518         return tomRam8[INT1 + 1/*0xE1*/] & (1 << irq);
1519 }
1520
1521 //unused
1522 /*void tom_set_irq_latch(int irq, int enabled)
1523 {
1524         tomRam8[0xE0] = (tomRam8[0xE0] & (~(1<<irq))) | (enabled ? (1<<irq) : 0);
1525 }*/
1526
1527 //unused
1528 /*uint16 tom_irq_control_reg(void)
1529 {
1530         return (tomRam8[0xE0] << 8) | tomRam8[0xE1];
1531 }*/
1532
1533 // NEW:
1534 // TOM Programmable Interrupt Timer handler
1535 // NOTE: TOM's PIT is only enabled if the prescaler is != 0
1536 //       The PIT only generates an interrupt when it counts down to zero, not when loaded!
1537
1538 void TOMPITCallback(void);
1539
1540 void TOMResetPIT(void)
1541 {
1542 #ifndef NEW_TIMER_SYSTEM
1543 //Probably should *add* this amount to the counter to retain cycle accuracy! !!! FIX !!! [DONE]
1544 //Also, why +1??? 'Cause that's what it says in the JTRM...!
1545 //There is a small problem with this approach: If both the prescaler and the divider are equal
1546 //to $FFFF then the counter won't be large enough to handle it. !!! FIX !!!
1547         if (tom_timer_prescaler)
1548                 tom_timer_counter += (1 + tom_timer_prescaler) * (1 + tom_timer_divider);
1549 //      WriteLog("tom: reseting timer to 0x%.8x (%i)\n",tom_timer_counter,tom_timer_counter);
1550 #else
1551         // Need to remove previous timer from the queue, if it exists...
1552         RemoveCallback(TOMPITCallback);
1553
1554         if (tomTimerPrescaler)
1555         {
1556                 double usecs = (float)(tomTimerPrescaler + 1) * (float)(tomTimerDivider + 1) * RISC_CYCLE_IN_USEC;
1557                 SetCallbackTime(TOMPITCallback, usecs);
1558         }
1559 #endif
1560 }
1561
1562 //
1563 // TOM Programmable Interrupt Timer handler
1564 // NOTE: TOM's PIT is only enabled if the prescaler is != 0
1565 //
1566 //NOTE: This is only used by the old execution code... Safe to remove
1567 //      once the timer system is stable.
1568 void TOMExecPIT(uint32 cycles)
1569 {
1570         if (tomTimerPrescaler)
1571         {
1572                 tomTimerCounter -= cycles;
1573
1574                 if (tomTimerCounter <= 0)
1575                 {
1576                         TOMSetPendingTimerInt();
1577                         GPUSetIRQLine(GPUIRQ_TIMER, ASSERT_LINE);       // GPUSetIRQLine does the 'IRQ enabled' checking
1578
1579                         if (TOMIRQEnabled(IRQ_TIMER))
1580                                 m68k_set_irq(7);                                // Cause a 68000 NMI...
1581
1582                         TOMResetPIT();
1583                 }
1584         }
1585 }
1586
1587
1588 void TOMPITCallback(void)
1589 {
1590 //      INT1_RREG |= 0x08;                         // Set TOM PIT interrupt pending
1591         TOMSetPendingTimerInt();
1592     GPUSetIRQLine(GPUIRQ_TIMER, ASSERT_LINE);  // It does the 'IRQ enabled' checking
1593
1594 //      if (INT1_WREG & 0x08)
1595         if (TOMIRQEnabled(IRQ_TIMER))
1596                 m68k_set_irq(7);                       // Generate 68K NMI
1597
1598         TOMResetPIT();
1599 }