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