]> Shamusworld >> Repos - virtualjaguar/blob - src/tom.cpp
Attempting to add sanity to memory access: Added mmu.cpp/h
[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 // 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 "tom.h"
248
249 #include <string.h>                                                             // For memset()
250 #include <stdlib.h>                                                             // For rand()
251 #include "blitter.h"
252 #include "cry2rgb.h"
253 #include "event.h"
254 #include "gpu.h"
255 #include "jaguar.h"
256 #include "log.h"
257 #include "m68k.h"
258 #include "objectp.h"
259 #include "settings.h"
260 #include "video.h"
261
262 #define NEW_TIMER_SYSTEM
263
264 // TOM registers (offset from $F00000)
265
266 #define MEMCON1         0x00
267 #define MEMCON2         0x02
268 #define HC                      0x04
269 #define VC                      0x06
270 #define VMODE           0x28
271 #define   MODE          0x0006          // Line buffer to video generator mode
272 #define   BGEN          0x0080          // Background enable (CRY & RGB16 only)
273 #define   VARMOD        0x0100          // Mixed CRY/RGB16 mode (only works in MODE 0!)
274 #define   PWIDTH        0x0E00          // Pixel width in video clock cycles (value written + 1)
275 #define BORD1           0x2A            // Border green/red values (8 BPP)
276 #define BORD2           0x2C            // Border blue value (8 BPP)
277 #define HP                      0x2E            // Values range from 1 - 1024 (value written + 1)
278 #define HBB                     0x30
279 #define HBE                     0x32
280 #define HDB1            0x38            // Horizontal display begin 1
281 #define HDB2            0x3A
282 #define HDE                     0x3C
283 #define VP                      0x3E            // Value ranges from 1 - 2048 (value written + 1)
284 #define VBB                     0x40
285 #define VBE                     0x42
286 #define VS                      0x44
287 #define VDB                     0x46
288 #define VDE                     0x48
289 #define VI                      0x4E
290 #define BG                      0x58
291 #define INT1            0xE0
292
293 //NOTE: These arbitrary cutoffs are NOT taken into account for PAL jaguar screens. !!! FIX !!!
294
295 // Arbitrary video cutoff values (i.e., first/last visible spots on a TV, in HC ticks)
296 /*#define LEFT_VISIBLE_HC                       208
297 #define RIGHT_VISIBLE_HC                1528//*/
298 #define LEFT_VISIBLE_HC                 208
299 #define RIGHT_VISIBLE_HC                1488
300 //#define TOP_VISIBLE_VC                25
301 //#define BOTTOM_VISIBLE_VC             503
302 #define TOP_VISIBLE_VC                  31
303 #define BOTTOM_VISIBLE_VC               511
304
305 //Are these PAL horizontals correct?
306 //They seem to be for the most part, but there are some games that seem to be
307 //shifted over to the right from this "window".
308 #define LEFT_VISIBLE_HC_PAL             208
309 #define RIGHT_VISIBLE_HC_PAL    1488
310 #define TOP_VISIBLE_VC_PAL              67
311 #define BOTTOM_VISIBLE_VC_PAL   579
312
313 //This can be defined in the makefile as well...
314 //(It's easier to do it here, though...)
315 //#define TOM_DEBUG
316
317 uint8 tomRam8[0x4000];
318 uint32 tomWidth, tomHeight;
319 uint32 tomTimerPrescaler;
320 uint32 tomTimerDivider;
321 int32 tomTimerCounter;
322 //uint32 tom_scanline;
323 //uint32 hblankWidthInPixels = 0;
324 uint16 tom_jerry_int_pending, tom_timer_int_pending, tom_object_int_pending,
325         tom_gpu_int_pending, tom_video_int_pending;
326 //uint16 * tom_cry_rgb_mix_lut;
327 //int16 * TOMBackbuffer;
328 uint32 * TOMBackbuffer;
329
330 static const char * videoMode_to_str[8] =
331         { "16 BPP CRY", "24 BPP RGB", "16 BPP DIRECT", "16 BPP RGB",
332           "Mixed mode", "24 BPP RGB", "16 BPP DIRECT", "16 BPP RGB" };
333
334 typedef void (render_xxx_scanline_fn)(uint32 *);
335
336 // Private function prototypes
337
338 void tom_render_16bpp_cry_scanline(uint32 * backbuffer);
339 void tom_render_24bpp_scanline(uint32 * backbuffer);
340 void tom_render_16bpp_direct_scanline(uint32 * backbuffer);
341 void tom_render_16bpp_rgb_scanline(uint32 * backbuffer);
342 void tom_render_16bpp_cry_rgb_mix_scanline(uint32 * backbuffer);
343
344 void tom_render_16bpp_cry_stretch_scanline(uint32 * backbuffer);
345 void tom_render_24bpp_stretch_scanline(uint32 * backbuffer);
346 void tom_render_16bpp_direct_stretch_scanline(uint32 * backbuffer);
347 void tom_render_16bpp_rgb_stretch_scanline(uint32 * backbuffer);
348 void tom_render_16bpp_cry_rgb_mix_stretch_scanline(uint32 * backbuffer);
349
350 render_xxx_scanline_fn * scanline_render_normal[] =
351 {
352         tom_render_16bpp_cry_scanline,
353         tom_render_24bpp_scanline,
354         tom_render_16bpp_direct_scanline,
355         tom_render_16bpp_rgb_scanline,
356         tom_render_16bpp_cry_rgb_mix_scanline,
357         tom_render_24bpp_scanline,
358         tom_render_16bpp_direct_scanline,
359         tom_render_16bpp_rgb_scanline
360 };
361
362 render_xxx_scanline_fn * scanline_render_stretch[] =
363 {
364         tom_render_16bpp_cry_stretch_scanline,
365         tom_render_24bpp_stretch_scanline,
366         tom_render_16bpp_direct_stretch_scanline,
367         tom_render_16bpp_rgb_stretch_scanline,
368         tom_render_16bpp_cry_rgb_mix_stretch_scanline,
369         tom_render_24bpp_stretch_scanline,
370         tom_render_16bpp_direct_stretch_scanline,
371         tom_render_16bpp_rgb_stretch_scanline,
372 };
373
374 render_xxx_scanline_fn * scanline_render[8];
375
376
377 // Screen info for various games [PAL]...
378 /*
379 BIOS
380 TOM: Horizontal Period written by M68K: 850 (+1*2 = 1702)
381 TOM: Horizontal Blank Begin written by M68K: 1711
382 TOM: Horizontal Blank End written by M68K: 158
383 TOM: Horizontal Display End written by M68K: 1696
384 TOM: Horizontal Display Begin 1 written by M68K: 166
385 TOM: Vertical Period written by M68K: 623 (non-interlaced)
386 TOM: Vertical Blank End written by M68K: 34
387 TOM: Vertical Display Begin written by M68K: 46
388 TOM: Vertical Display End written by M68K: 526
389 TOM: Vertical Blank Begin written by M68K: 600
390 TOM: Vertical Sync written by M68K: 618
391 TOM: Horizontal Display End written by M68K: 1665
392 TOM: Horizontal Display Begin 1 written by M68K: 203
393 TOM: Vertical Display Begin written by M68K: 38
394 TOM: Vertical Display End written by M68K: 518
395 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 151)
396 TOM: Horizontal Display End written by M68K: 1713
397 TOM: Horizontal Display Begin 1 written by M68K: 157
398 TOM: Vertical Display Begin written by M68K: 35
399 TOM: Vertical Display End written by M68K: 2047
400 Horizontal range: 157 - 1713 (width: 1557 / 4 = 389.25, / 5 = 315.4)
401
402 Asteroid
403 TOM: Horizontal Period written by M68K: 845 (+1*2 = 1692)
404 TOM: Horizontal Blank Begin written by M68K: 1700
405 TOM: Horizontal Blank End written by M68K: 122
406 TOM: Horizontal Display End written by M68K: 1600
407 TOM: Horizontal Display Begin 1 written by M68K: 268
408 TOM: Vertical Period written by M68K: 523 (non-interlaced)
409 TOM: Vertical Blank End written by M68K: 40
410 TOM: Vertical Display Begin written by M68K: 44
411 TOM: Vertical Display End written by M68K: 492
412 TOM: Vertical Blank Begin written by M68K: 532
413 TOM: Vertical Sync written by M68K: 513
414 TOM: Video Mode written by M68K: 04C7. PWIDTH = 3, MODE = 16 BPP RGB, flags: BGEN (VC = 461)
415
416 Rayman
417 TOM: Horizontal Display End written by M68K: 1713
418 TOM: Horizontal Display Begin 1 written by M68K: 157
419 TOM: Vertical Display Begin written by M68K: 35
420 TOM: Vertical Display End written by M68K: 2047
421 TOM: Video Mode written by M68K: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 89)
422 TOM: Horizontal Display Begin 1 written by M68K: 208
423 TOM: Horizontal Display End written by M68K: 1662
424 TOM: Vertical Display Begin written by M68K: 100
425 TOM: Vertical Display End written by M68K: 2047
426 TOM: Video Mode written by M68K: 07C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN VARMOD (VC = 205)
427 Horizontal range: 208 - 1662 (width: 1455 / 4 = 363.5)
428
429 Alien vs Predator
430 TOM: Vertical Display Begin written by M68K: 96
431 TOM: Vertical Display End written by M68K: 2047
432 TOM: Horizontal Display Begin 1 written by M68K: 239
433 TOM: Horizontal Display End written by M68K: 1692
434 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 378)
435 TOM: Vertical Display Begin written by M68K: 44
436 TOM: Vertical Display End written by M68K: 2047
437 TOM: Horizontal Display Begin 1 written by M68K: 239
438 TOM: Horizontal Display End written by M68K: 1692
439 TOM: Video Mode written by M68K: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 559)
440 TOM: Vertical Display Begin written by M68K: 84
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 TOM: Vertical Display Begin written by M68K: 44
445 TOM: Vertical Display End written by M68K: 2047
446 TOM: Horizontal Display Begin 1 written by M68K: 239
447 TOM: Horizontal Display End written by M68K: 1692
448 Horizontal range: 239 - 1692 (width: 1454 / 4 = 363.5)
449
450 */
451
452 // Screen info for various games [NTSC]...
453 /*
454 Doom
455 TOM: Horizontal Display End written by M68K: 1727
456 TOM: Horizontal Display Begin 1 written by M68K: 123
457 TOM: Vertical Display Begin written by M68K: 25
458 TOM: Vertical Display End written by M68K: 2047
459 TOM: Video Mode written by M68K: 0EC1. PWIDTH = 8, MODE = 16 BPP CRY, flags: BGEN (VC = 5)
460 Also does PWIDTH = 4...
461 Vertical resolution: 238 lines
462
463 Rayman
464 TOM: Horizontal Display End written by M68K: 1727
465 TOM: Horizontal Display Begin 1 written by M68K: 123
466 TOM: Vertical Display Begin written by M68K: 25
467 TOM: Vertical Display End written by M68K: 2047
468 TOM: Vertical Interrupt written by M68K: 507
469 TOM: Video Mode written by M68K: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 92)
470 TOM: Horizontal Display Begin 1 written by M68K: 208
471 TOM: Horizontal Display End written by M68K: 1670
472 Display starts at 31, then 52!
473 Vertical resolution: 238 lines
474
475 Atari Karts
476 TOM: Horizontal Display End written by M68K: 1727
477 TOM: Horizontal Display Begin 1 written by M68K: 123
478 TOM: Vertical Display Begin written by M68K: 25
479 TOM: Vertical Display End written by M68K: 2047
480 TOM: Video Mode written by GPU: 08C7. PWIDTH = 5, MODE = 16 BPP RGB, flags: BGEN (VC = 4)
481 TOM: Video Mode written by GPU: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 508)
482 Display starts at 31 (PWIDTH = 4), 24 (PWIDTH = 5)
483
484 Iron Soldier
485 TOM: Vertical Interrupt written by M68K: 2047
486 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 0)
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: Vertical Interrupt written by M68K: 507
492 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 369)
493 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 510)
494 TOM: Video Mode written by M68K: 06C3. PWIDTH = 4, MODE = 24 BPP RGB, flags: BGEN (VC = 510)
495 Display starts at 31
496 Vertical resolution: 238 lines
497 [Seems to be a problem between the horizontal positioning of the 16-bit CRY & 24-bit RGB]
498
499 JagMania
500 TOM: Horizontal Period written by M68K: 844 (+1*2 = 1690)
501 TOM: Horizontal Blank Begin written by M68K: 1713
502 TOM: Horizontal Blank End written by M68K: 125
503 TOM: Horizontal Display End written by M68K: 1696
504 TOM: Horizontal Display Begin 1 written by M68K: 166
505 TOM: Vertical Period written by M68K: 523 (non-interlaced)
506 TOM: Vertical Blank End written by M68K: 24
507 TOM: Vertical Display Begin written by M68K: 46
508 TOM: Vertical Display End written by M68K: 496
509 TOM: Vertical Blank Begin written by M68K: 500
510 TOM: Vertical Sync written by M68K: 517
511 TOM: Vertical Interrupt written by M68K: 497
512 TOM: Video Mode written by M68K: 04C1. PWIDTH = 3, MODE = 16 BPP CRY, flags: BGEN (VC = 270)
513 Display starts at 55
514
515 Double Dragon V
516 TOM: Horizontal Display End written by M68K: 1727
517 TOM: Horizontal Display Begin 1 written by M68K: 123
518 TOM: Vertical Display Begin written by M68K: 25
519 TOM: Vertical Display End written by M68K: 2047
520 TOM: Vertical Interrupt written by M68K: 507
521 TOM: Video Mode written by M68K: 06C7. PWIDTH = 4, MODE = 16 BPP RGB, flags: BGEN (VC = 9)
522
523 Dino Dudes
524 TOM: Horizontal Display End written by M68K: 1823
525 TOM: Horizontal Display Begin 1 written by M68K: 45
526 TOM: Vertical Display Begin written by M68K: 40
527 TOM: Vertical Display End written by M68K: 2047
528 TOM: Vertical Interrupt written by M68K: 491
529 TOM: Video Mode written by M68K: 06C1. PWIDTH = 4, MODE = 16 BPP CRY, flags: BGEN (VC = 398)
530 Display starts at 11 (123 - 45 = 78, 78 / 4 = 19 pixels to skip)
531 Width is 417, so maybe width of 379 would be good (starting at 123, ending at 1639)
532 Vertical resolution: 238 lines
533
534 Flashback
535 TOM: Horizontal Display End written by M68K: 1727
536 TOM: Horizontal Display Begin 1 written by M68K: 188
537 TOM: Vertical Display Begin written by M68K: 1
538 TOM: Vertical Display End written by M68K: 2047
539 TOM: Vertical Interrupt written by M68K: 483
540 TOM: Video Mode written by M68K: 08C7. PWIDTH = 5, MODE = 16 BPP RGB, flags: BGEN (VC = 99)
541 Width would be 303 with above scheme, but border width would be 13 pixels
542
543 Trevor McFur
544 Vertical resolution: 238 lines
545 */
546
547 uint32 RGB16ToRGB32[0x10000];
548 uint32 CRY16ToRGB32[0x10000];
549 uint32 MIX16ToRGB32[0x10000];
550
551 void TOMFillLookupTables(void)
552 {
553         for(uint32 i=0; i<0x10000; i++)
554                 RGB16ToRGB32[i] = 0xFF000000
555                         | ((i & 0xF100) >> 8)  | ((i & 0xE000) >> 13)
556                         | ((i & 0x07C0) << 13) | ((i & 0x0700) << 8)
557                         | ((i & 0x003F) << 10) | ((i & 0x0030) << 4);
558
559
560         for(uint32 i=0; i<0x10000; i++)
561         {
562                 uint32 chrm = (i & 0xF000) >> 12,
563                         chrl = (i & 0x0F00) >> 8,
564                         y = (i & 0x00FF);
565
566                 uint32 r = (((uint32)redcv[chrm][chrl]) * y) >> 8,
567                         g = (((uint32)greencv[chrm][chrl]) * y) >> 8,
568                         b = (((uint32)bluecv[chrm][chrl]) * y) >> 8;
569
570                 CRY16ToRGB32[i] = 0xFF000000 | (b << 16) | (g << 8) | r;
571                 MIX16ToRGB32[i] = CRY16ToRGB32[i];
572         }
573
574         for(uint32 i=0; i<0x10000; i++)
575                 if (i & 0x01)
576                         MIX16ToRGB32[i] = RGB16ToRGB32[i];
577 }
578
579 void TOMSetPendingJERRYInt(void)
580 {
581         tom_jerry_int_pending = 1;
582 }
583
584 void TOMSetPendingTimerInt(void)
585 {
586         tom_timer_int_pending = 1;
587 }
588
589 void TOMSetPendingObjectInt(void)
590 {
591         tom_object_int_pending = 1;
592 }
593
594 void TOMSetPendingGPUInt(void)
595 {
596         tom_gpu_int_pending = 1;
597 }
598
599 void TOMSetPendingVideoInt(void)
600 {
601         tom_video_int_pending = 1;
602 }
603
604 uint8 * TOMGetRamPointer(void)
605 {
606         return tomRam8;
607 }
608
609 uint8 TOMGetVideoMode(void)
610 {
611         uint16 vmode = GET16(tomRam8, VMODE);
612         return ((vmode & VARMOD) >> 6) | ((vmode & MODE) >> 1);
613 }
614
615 //Used in only one place (and for debug purposes): OBJECTP.CPP
616 uint16 TOMGetVDB(void)
617 {
618 // This in NOT VDB!!!
619 //      return GET16(tomRam8, VBE);
620         return GET16(tomRam8, VDB);
621 }
622
623 //
624 // 16 BPP CRY/RGB mixed mode rendering
625 //
626 void tom_render_16bpp_cry_rgb_mix_scanline(uint32 * backbuffer)
627 {
628 //CHANGED TO 32BPP RENDERING
629         uint16 width = tomWidth;
630         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
631
632         //New stuff--restrict our drawing...
633         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
634         //NOTE: May have to check HDB2 as well!
635         // Get start position in HC ticks
636         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL);
637         startPos /= pwidth;
638         if (startPos < 0)
639                 current_line_buffer += 2 * -startPos;
640         else
641 //This case doesn't properly handle the "start on the right side of virtual screen" case
642 //Dunno why--looks Ok...
643 //What *is* for sure wrong is that it doesn't copy the linebuffer's BG pixels...
644 //This should likely be 4 instead of 2 (?--not sure)
645                 backbuffer += 2 * startPos, width -= startPos;
646
647         while (width)
648         {
649                 uint16 color = (*current_line_buffer++) << 8;
650                 color |= *current_line_buffer++;
651                 *backbuffer++ = MIX16ToRGB32[color];
652                 width--;
653         }
654 }
655
656 //
657 // 16 BPP CRY mode rendering
658 //
659 void tom_render_16bpp_cry_scanline(uint32 * backbuffer)
660 {
661 //CHANGED TO 32BPP RENDERING
662         uint16 width = tomWidth;
663         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
664
665         //New stuff--restrict our drawing...
666         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
667         //NOTE: May have to check HDB2 as well!
668         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL);// Get start position in HC ticks
669         startPos /= pwidth;
670         if (startPos < 0)
671                 current_line_buffer += 2 * -startPos;
672         else
673 //This should likely be 4 instead of 2 (?--not sure)
674                 backbuffer += 2 * startPos, width -= startPos;
675
676         while (width)
677         {
678                 uint16 color = (*current_line_buffer++) << 8;
679                 color |= *current_line_buffer++;
680                 *backbuffer++ = CRY16ToRGB32[color];
681                 width--;
682         }
683 }
684
685 //
686 // 24 BPP mode rendering
687 //
688 void tom_render_24bpp_scanline(uint32 * backbuffer)
689 {
690 //CHANGED TO 32BPP RENDERING
691         uint16 width = tomWidth;
692         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
693
694         //New stuff--restrict our drawing...
695         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
696         //NOTE: May have to check HDB2 as well!
697         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL); // Get start position in HC ticks
698         startPos /= pwidth;
699         if (startPos < 0)
700                 current_line_buffer += 4 * -startPos;
701         else
702 //This should likely be 4 instead of 2 (?--not sure)
703                 backbuffer += 2 * startPos, width -= startPos;
704
705         while (width)
706         {
707                 uint32 g = *current_line_buffer++;
708                 uint32 r = *current_line_buffer++;
709                 current_line_buffer++;
710                 uint32 b = *current_line_buffer++;
711                 *backbuffer++ = 0xFF000000 | (b << 16) | (g << 8) | r;
712                 width--;
713         }
714 }
715
716 //Seems to me that this is NOT a valid mode--the JTRM seems to imply that you would need
717 //extra hardware outside of the Jaguar console to support this!
718 //
719 // 16 BPP direct mode rendering
720 //
721 void tom_render_16bpp_direct_scanline(uint32 * backbuffer)
722 {
723         uint16 width = tomWidth;
724         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
725
726         while (width)
727         {
728                 uint16 color = (*current_line_buffer++) << 8;
729                 color |= *current_line_buffer++;
730                 *backbuffer++ = color >> 1;
731                 width--;
732         }
733 }
734
735 //
736 // 16 BPP RGB mode rendering
737 //
738 void tom_render_16bpp_rgb_scanline(uint32 * backbuffer)
739 {
740 //CHANGED TO 32BPP RENDERING
741         // 16 BPP RGB: 0-5 green, 6-10 blue, 11-15 red
742
743         uint16 width = tomWidth;
744         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
745
746         //New stuff--restrict our drawing...
747         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
748         //NOTE: May have to check HDB2 as well!
749         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL); // Get start position in HC ticks
750         startPos /= pwidth;
751
752         if (startPos < 0)
753                 current_line_buffer += 2 * -startPos;
754         else
755 //This should likely be 4 instead of 2 (?--not sure)
756                 backbuffer += 2 * startPos, width -= startPos;
757
758         while (width)
759         {
760                 uint32 color = (*current_line_buffer++) << 8;
761                 color |= *current_line_buffer++;
762                 *backbuffer++ = RGB16ToRGB32[color];
763                 width--;
764         }
765 }
766
767 /////////////////////////////////////////////////////////////////////
768 // This stuff may just go away by itself, especially if we do some //
769 // good old OpenGL goodness...                                     //
770 /////////////////////////////////////////////////////////////////////
771
772 void tom_render_16bpp_cry_rgb_mix_stretch_scanline(uint32 *backbuffer)
773 {
774         uint16 width=tomWidth;
775         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
776
777         while (width)
778         {
779                 uint16 color = *current_line_buffer++;
780                 color <<= 8;
781                 color |= *current_line_buffer++;
782                 *backbuffer++ = MIX16ToRGB32[color];
783                 current_line_buffer += 2;
784                 width--;
785         }
786 }
787
788 void tom_render_16bpp_cry_stretch_scanline(uint32 *backbuffer)
789 {
790         uint32 chrm, chrl, y;
791
792         uint16 width=tomWidth;
793         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
794
795         while (width)
796         {
797                 uint16 color;
798                 color=*current_line_buffer++;
799                 color<<=8;
800                 color|=*current_line_buffer++;
801
802                 chrm = (color & 0xF000) >> 12;
803                 chrl = (color & 0x0F00) >> 8;
804                 y    = (color & 0x00FF);
805
806                 uint16 red   =  ((((uint32)redcv[chrm][chrl])*y)>>11);
807                 uint16 green =  ((((uint32)greencv[chrm][chrl])*y)>>11);
808                 uint16 blue  =  ((((uint32)bluecv[chrm][chrl])*y)>>11);
809
810                 uint16 color2;
811                 color2=*current_line_buffer++;
812                 color2<<=8;
813                 color2|=*current_line_buffer++;
814
815                 chrm = (color2 & 0xF000) >> 12;
816                 chrl = (color2 & 0x0F00) >> 8;
817                 y    = (color2 & 0x00FF);
818
819                 uint16 red2   = ((((uint32)redcv[chrm][chrl])*y)>>11);
820                 uint16 green2 = ((((uint32)greencv[chrm][chrl])*y)>>11);
821                 uint16 blue2  = ((((uint32)bluecv[chrm][chrl])*y)>>11);
822
823                 red=(red+red2)>>1;
824                 green=(green+green2)>>1;
825                 blue=(blue+blue2)>>1;
826
827                 *backbuffer++=(red<<10)|(green<<5)|blue;
828                 width--;
829         }
830 }
831
832 void tom_render_24bpp_stretch_scanline(uint32 *backbuffer)
833 {
834         uint16 width=tomWidth;
835         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
836
837         while (width)
838         {
839                 uint16 green=*current_line_buffer++;
840                 uint16 red=*current_line_buffer++;
841                 /*uint16 nc=*/current_line_buffer++;
842                 uint16 blue=*current_line_buffer++;
843                 red>>=3;
844                 green>>=3;
845                 blue>>=3;
846                 *backbuffer++=(red<<10)|(green<<5)|blue;
847                 current_line_buffer+=4;
848                 width--;
849         }
850 }
851
852 void tom_render_16bpp_direct_stretch_scanline(uint32 *backbuffer)
853 {
854         uint16 width=tomWidth;
855         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
856
857         while (width)
858         {
859                 uint16 color=*current_line_buffer++;
860                 color<<=8;
861                 color|=*current_line_buffer++;
862                 color>>=1;
863                 *backbuffer++=color;
864                 current_line_buffer+=2;
865                 width--;
866         }
867 }
868
869 void tom_render_16bpp_rgb_stretch_scanline(uint32 *backbuffer)
870 {
871         uint16 width=tomWidth;
872         uint8 *current_line_buffer=(uint8*)&tomRam8[0x1800];
873
874         while (width)
875         {
876                 uint16 color1=*current_line_buffer++;
877                 color1<<=8;
878                 color1|=*current_line_buffer++;
879                 color1>>=1;
880                 uint16 color2=*current_line_buffer++;
881                 color2<<=8;
882                 color2|=*current_line_buffer++;
883                 color2>>=1;
884                 uint16 red=(((color1&0x7c00)>>10)+((color2&0x7c00)>>10))>>1;
885                 uint16 green=(((color1&0x00003e0)>>5)+((color2&0x00003e0)>>5))>>1;
886                 uint16 blue=(((color1&0x0000001f))+((color2&0x0000001f)))>>1;
887
888                 color1=(red<<10)|(blue<<5)|green;
889                 *backbuffer++=color1;
890                 width--;
891         }
892 }
893
894 void TOMResetBackbuffer(uint32 * backbuffer)
895 {
896         TOMBackbuffer = backbuffer;
897 }
898
899 //
900 // Process a single scanline
901 //
902 void TOMExecScanline(uint16 scanline, bool render)
903 {
904         bool inActiveDisplayArea = true;
905
906 //Interlacing is still not handled correctly here... !!! FIX !!!
907         if (scanline & 0x01)                                                    // Execute OP only on even lines (non-interlaced only!)
908                 return;
909
910         if (scanline >= (uint16)GET16(tomRam8, VDB) && scanline < (uint16)GET16(tomRam8, VDE))
911         {
912                 if (render)
913                 {
914                         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
915                         uint8 bgHI = tomRam8[BG], bgLO = tomRam8[BG + 1];
916
917                         // Clear line buffer with BG
918                         if (GET16(tomRam8, VMODE) & BGEN) // && (CRY or RGB16)...
919                                 for(uint32 i=0; i<720; i++)
920                                         *current_line_buffer++ = bgHI, *current_line_buffer++ = bgLO;
921
922                         OPProcessList(scanline, render);
923                 }
924         }
925         else
926                 inActiveDisplayArea = false;
927
928         // Try to take PAL into account...
929
930         uint16 topVisible = (vjs.hardwareTypeNTSC ? TOP_VISIBLE_VC : TOP_VISIBLE_VC_PAL),
931                 bottomVisible = (vjs.hardwareTypeNTSC ? BOTTOM_VISIBLE_VC : BOTTOM_VISIBLE_VC_PAL);
932
933         // Here's our virtualized scanline code...
934
935         if (scanline >= topVisible && scanline < bottomVisible)
936         {
937                 if (inActiveDisplayArea)
938                 {
939 //NOTE: The following doesn't put BORDER color on the sides... !!! FIX !!!
940 #warning The following doesn't put BORDER color on the sides... !!! FIX !!!
941                         if (vjs.renderType == RT_NORMAL)
942                                 scanline_render[TOMGetVideoMode()](TOMBackbuffer);
943                         else//TV type render
944                         {
945 /*
946         tom_render_16bpp_cry_scanline,
947         tom_render_24bpp_scanline,
948         tom_render_16bpp_direct_scanline,
949         tom_render_16bpp_rgb_scanline,
950         tom_render_16bpp_cry_rgb_mix_scanline,
951         tom_render_24bpp_scanline,
952         tom_render_16bpp_direct_scanline,
953         tom_render_16bpp_rgb_scanline
954 #define VMODE           0x28
955 #define   MODE          0x0006          // Line buffer to video generator mode
956 #define   VARMOD        0x0100          // Mixed CRY/RGB16 mode (only works in MODE 0!)
957 */
958                                 uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
959                                 uint8 mode = ((GET16(tomRam8, VMODE) & MODE) >> 1);
960                                 bool varmod = GET16(tomRam8, VMODE) & VARMOD;
961 //The video texture line buffer ranges from 0 to 1279, with its left edge starting at
962 //LEFT_VISIBLE_HC. So, we need to start writing into the backbuffer at HDB1, using pwidth
963 //as our scaling factor. The way it generates its image on a real TV!
964
965 //So, for example, if HDB1 is less than LEFT_VISIBLE_HC, then we have to figure out where
966 //in the VTLB that we start writing pixels from the Jaguar line buffer (VTLB start=0,
967 //JLB=something).
968 #if 0
969 //
970 // 24 BPP mode rendering
971 //
972 void tom_render_24bpp_scanline(uint32 * backbuffer)
973 {
974 //CHANGED TO 32BPP RENDERING
975         uint16 width = tomWidth;
976         uint8 * current_line_buffer = (uint8 *)&tomRam8[0x1800];
977
978         //New stuff--restrict our drawing...
979         uint8 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
980         //NOTE: May have to check HDB2 as well!
981         int16 startPos = GET16(tomRam8, HDB1) - (vjs.hardwareTypeNTSC ? LEFT_VISIBLE_HC : LEFT_VISIBLE_HC_PAL); // Get start position in HC ticks
982         startPos /= pwidth;
983         if (startPos < 0)
984                 current_line_buffer += 4 * -startPos;
985         else
986 //This should likely be 4 instead of 2 (?--not sure)
987                 backbuffer += 2 * startPos, width -= startPos;
988
989         while (width)
990         {
991                 uint32 g = *current_line_buffer++;
992                 uint32 r = *current_line_buffer++;
993                 current_line_buffer++;
994                 uint32 b = *current_line_buffer++;
995                 *backbuffer++ = 0xFF000000 | (b << 16) | (g << 8) | r;
996                 width--;
997         }
998 }
999 #endif
1000
1001                         }
1002                 }
1003                 else
1004                 {
1005                         // If outside of VDB & VDE, then display the border color
1006                         uint32 * currentLineBuffer = TOMBackbuffer;
1007                         uint8 g = tomRam8[BORD1], r = tomRam8[BORD1 + 1], b = tomRam8[BORD2 + 1];
1008                         uint32 pixel = 0xFF000000 | (b << 16) | (g << 8) | r;
1009
1010                         for(uint32 i=0; i<tomWidth; i++)
1011                                 *currentLineBuffer++ = pixel;
1012                 }
1013
1014                 TOMBackbuffer += GetSDLScreenWidthInPixels();
1015         }
1016 }
1017
1018 //
1019 // TOM initialization
1020 //
1021 void TOMInit(void)
1022 {
1023         OPInit();
1024         BlitterInit();
1025         TOMReset();
1026         // Setup the non-stretchy scanline rendering...
1027         memcpy(scanline_render, scanline_render_normal, sizeof(scanline_render));
1028         TOMFillLookupTables();
1029 }
1030
1031 void TOMDone(void)
1032 {
1033         OPDone();
1034         BlitterDone();
1035         WriteLog("TOM: Resolution %i x %i %s\n", TOMGetVideoModeWidth(), TOMGetVideoModeHeight(),
1036                 videoMode_to_str[TOMGetVideoMode()]);
1037 //      WriteLog("\ntom: object processor:\n");
1038 //      WriteLog("tom: pointer to object list: 0x%.8x\n",op_get_list_pointer());
1039 //      WriteLog("tom: INT1=0x%.2x%.2x\n",TOMReadByte(0xf000e0),TOMReadByte(0xf000e1));
1040 //      gpu_done();
1041 //      dsp_done();
1042 //      memory_free(tomRam8);
1043 //      memory_free(tom_cry_rgb_mix_lut);
1044 }
1045
1046 /*uint32 tom_getHBlankWidthInPixels(void)
1047 {
1048         return hblankWidthInPixels;
1049 }*/
1050
1051 uint32 TOMGetVideoModeWidth(void)
1052 {
1053         //These widths are pretty bogus. Should use HDB1/2 & HDE/HBB & PWIDTH to calc the width...
1054 //      uint32 width[8] = { 1330, 665, 443, 332, 266, 222, 190, 166 };
1055 //Temporary, for testing Doom...
1056 //      uint32 width[8] = { 1330, 665, 443, 332, 266, 222, 190, 332 };
1057
1058         // Note that the following PWIDTH values have the following pixel aspect ratios:
1059         // PWIDTH = 1 -> 0.25:1 (1:4) pixels (X:Y ratio)
1060         // PWIDTH = 2 -> 0.50:1 (1:2) pixels
1061         // PWIDTH = 3 -> 0.75:1 (3:4) pixels
1062         // PWIDTH = 4 -> 1.00:1 (1:1) pixels
1063         // PWIDTH = 5 -> 1.25:1 (5:4) pixels
1064         // PWIDTH = 6 -> 1.50:1 (3:2) pixels
1065         // PWIDTH = 7 -> 1.75:1 (7:4) pixels
1066         // PWIDTH = 8 -> 2.00:1 (2:1) pixels
1067
1068         // Also note that the JTRM says that PWIDTH of 4 gives pixels that are "about" square--
1069         // this implies that the other modes have pixels that are *not* square!
1070         // Also, I seriously doubt that you will see any games that use PWIDTH = 1!
1071
1072         // NOTE: Even though the PWIDTH value is + 1, here we're using a zero-based index and
1073         //       so we don't bother to add one...
1074 //      return width[(GET16(tomRam8, VMODE) & PWIDTH) >> 9];
1075
1076         // Now, we just calculate it...
1077 /*      uint16 hdb1 = GET16(tomRam8, HDB1), hde = GET16(tomRam8, HDE),
1078                 hbb = GET16(tomRam8, HBB), pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
1079 //      return ((hbb < hde ? hbb : hde) - hdb1) / pwidth;
1080 //Temporary, for testing Doom...
1081         return ((hbb < hde ? hbb : hde) - hdb1) / (pwidth == 8 ? 4 : pwidth);*/
1082
1083         // To make it easier to make a quasi-fixed display size, we restrict the viewing
1084         // area to an arbitrary range of the Horizontal Count.
1085         uint16 pwidth = ((GET16(tomRam8, VMODE) & PWIDTH) >> 9) + 1;
1086         return (vjs.hardwareTypeNTSC ? RIGHT_VISIBLE_HC - LEFT_VISIBLE_HC : RIGHT_VISIBLE_HC_PAL - LEFT_VISIBLE_HC_PAL) / pwidth;
1087 //Temporary, for testing Doom...
1088 //      return (RIGHT_VISIBLE_HC - LEFT_VISIBLE_HC) / (pwidth == 8 ? 4 : pwidth);
1089 ////    return (RIGHT_VISIBLE_HC - LEFT_VISIBLE_HC) / (pwidth == 4 ? 8 : pwidth);
1090
1091 // More speculating...
1092 // According to the JTRM, the number of potential pixels across is given by the
1093 // Horizontal Period (HP - in NTSC this is 845). The Horizontal Count counts from
1094 // zero to this value twice per scanline (the high bit is set on the second count).
1095 // HBE and HBB define the absolute "black" limits of the screen, while HDB1/2 and
1096 // HDE determine the extent of the OP "on" time. I.e., when the OP is turned on by
1097 // HDB1, it starts fetching the line from position 0 in LBUF.
1098
1099 // The trick, it would seem, is to figure out how long the typical visible scanline
1100 // of a TV is in HP ticks and limit the visible area to that (divided by PWIDTH, of
1101 // course). Using that length, we can establish an "absolute left display limit" with
1102 // which to measure HBB & HDB1/2 against when rendering LBUF (i.e., if HDB1 is 20 ticks
1103 // to the right of the ALDL and PWIDTH is 4, then start writing the LBUF starting at
1104 // backbuffer + 5 pixels).
1105
1106 // That's basically what we're doing now...!
1107 }
1108
1109 // *** SPECULATION ***
1110 // It might work better to virtualize the height settings, i.e., set the vertical
1111 // height at 240 lines and clip using the VDB and VDE/VP registers...
1112 // Same with the width... [Width is pretty much virtualized now.]
1113
1114 // Now that that the width is virtualized, let's virtualize the height. :-)
1115 uint32 TOMGetVideoModeHeight(void)
1116 {
1117 //      uint16 vmode = GET16(tomRam8, VMODE);
1118 //      uint16 vbe = GET16(tomRam8, VBE);
1119 //      uint16 vbb = GET16(tomRam8, VBB);
1120 //      uint16 vdb = GET16(tomRam8, VDB);
1121 //      uint16 vde = GET16(tomRam8, VDE);
1122 //      uint16 vp = GET16(tomRam8, VP);
1123
1124 /*      if (vde == 0xFFFF)
1125                 vde = vbb;//*/
1126
1127 //      return 227;//WAS:(vde/*-vdb*/) >> 1;
1128         // The video mode height probably works this way:
1129         // VC counts from 0 to VP. VDB starts the OP. Either when
1130         // VDE is reached or VP, the OP is stopped. Let's try it...
1131         // Also note that we're conveniently ignoring interlaced display modes...!
1132 //      return ((vde > vp ? vp : vde) - vdb) >> 1;
1133 //      return ((vde > vbb ? vbb : vde) - vdb) >> 1;
1134 //Let's try from the Vertical Blank interval...
1135 //Seems to work OK!
1136 //      return (vbb - vbe) >> 1;        // Again, doesn't take interlacing into account...
1137 // This of course doesn't take interlacing into account. But I haven't seen any
1138 // Jaguar software that takes advantage of it either...
1139 //Also, doesn't reflect PAL Jaguar either... !!! FIX !!! [DONE]
1140 //      return 240;                                                                             // Set virtual screen height to 240 lines...
1141         return (vjs.hardwareTypeNTSC ? 240 : 256);
1142 }
1143
1144 //
1145 // TOM reset code
1146 // Now PAL friendly!
1147 //
1148 void TOMReset(void)
1149 {
1150         OPReset();
1151         BlitterReset();
1152         memset(tomRam8, 0x00, 0x4000);
1153
1154         if (vjs.hardwareTypeNTSC)
1155         {
1156                 SET16(tomRam8, MEMCON1, 0x1861);
1157                 SET16(tomRam8, MEMCON2, 0x35CC);
1158                 SET16(tomRam8, HP, 844);                                // Horizontal Period (1-based; HP=845)
1159                 SET16(tomRam8, HBB, 1713);                      // Horizontal Blank Begin
1160                 SET16(tomRam8, HBE, 125);                               // Horizontal Blank End
1161                 SET16(tomRam8, HDE, 1665);                      // Horizontal Display End
1162                 SET16(tomRam8, HDB1, 203);                      // Horizontal Display Begin 1
1163                 SET16(tomRam8, VP, 523);                                // Vertical Period (1-based; in this case VP = 524)
1164                 SET16(tomRam8, VBE, 24);                                // Vertical Blank End
1165                 SET16(tomRam8, VDB, 38);                                // Vertical Display Begin
1166                 SET16(tomRam8, VDE, 518);                               // Vertical Display End
1167                 SET16(tomRam8, VBB, 500);                               // Vertical Blank Begin
1168                 SET16(tomRam8, VS, 517);                                // Vertical Sync
1169                 SET16(tomRam8, VMODE, 0x06C1);
1170         }
1171         else    // PAL Jaguar
1172         {
1173                 SET16(tomRam8, MEMCON1, 0x1861);
1174                 SET16(tomRam8, MEMCON2, 0x35CC);
1175                 SET16(tomRam8, HP, 850);                                // Horizontal Period
1176                 SET16(tomRam8, HBB, 1711);                      // Horizontal Blank Begin
1177                 SET16(tomRam8, HBE, 158);                               // Horizontal Blank End
1178                 SET16(tomRam8, HDE, 1665);                      // Horizontal Display End
1179                 SET16(tomRam8, HDB1, 203);                      // Horizontal Display Begin 1
1180                 SET16(tomRam8, VP, 623);                                // Vertical Period (1-based; in this case VP = 624)
1181                 SET16(tomRam8, VBE, 34);                                // Vertical Blank End
1182                 SET16(tomRam8, VDB, 38);                                // Vertical Display Begin
1183                 SET16(tomRam8, VDE, 518);                               // Vertical Display End
1184                 SET16(tomRam8, VBB, 600);                               // Vertical Blank Begin
1185                 SET16(tomRam8, VS, 618);                                // Vertical Sync
1186                 SET16(tomRam8, VMODE, 0x06C1);
1187         }
1188
1189         tomWidth = 0;
1190         tomHeight = 0;
1191
1192         tom_jerry_int_pending = 0;
1193         tom_timer_int_pending = 0;
1194         tom_object_int_pending = 0;
1195         tom_gpu_int_pending = 0;
1196         tom_video_int_pending = 0;
1197
1198         tom_timer_prescaler = 0;                                        // TOM PIT is disabled
1199         tom_timer_divider = 0;
1200         tom_timer_counter = 0;
1201         memcpy(scanline_render, scanline_render_normal, sizeof(scanline_render));
1202 }
1203
1204 //
1205 // TOM byte access (read)
1206 //
1207 uint8 TOMReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
1208 {
1209 //???Is this needed???
1210 // It seems so. Perhaps it's the +$8000 offset being written to (32-bit interface)?
1211 // However, the 32-bit interface is WRITE ONLY, so that can't be it...
1212 // Also, the 68K CANNOT make use of the 32-bit interface, since its bus width is only 16-bits...
1213 //      offset &= 0xFF3FFF;
1214
1215 #ifdef TOM_DEBUG
1216         WriteLog("TOM: Reading byte at %06X\n", offset);
1217 #endif
1218
1219         if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
1220                 return GPUReadByte(offset, who);
1221         else if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
1222                 return GPUReadByte(offset, who);
1223 /*      else if ((offset >= 0xF00010) && (offset < 0xF00028))
1224                 return OPReadByte(offset, who);*/
1225         else if ((offset >= 0xF02200) && (offset < 0xF022A0))
1226                 return BlitterReadByte(offset, who);
1227         else if (offset == 0xF00050)
1228                 return tom_timer_prescaler >> 8;
1229         else if (offset == 0xF00051)
1230                 return tom_timer_prescaler & 0xFF;
1231         else if (offset == 0xF00052)
1232                 return tom_timer_divider >> 8;
1233         else if (offset == 0xF00053)
1234                 return tom_timer_divider & 0xFF;
1235
1236         return tomRam8[offset & 0x3FFF];
1237 }
1238
1239 //
1240 // TOM word access (read)
1241 //
1242 uint16 TOMReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
1243 {
1244 //???Is this needed???
1245 //      offset &= 0xFF3FFF;
1246 #ifdef TOM_DEBUG
1247         WriteLog("TOM: Reading word at %06X\n", offset);
1248 #endif
1249 if (offset >= 0xF02000 && offset <= 0xF020FF)
1250         WriteLog("TOM: Read attempted from GPU register file by %s (unimplemented)!\n", whoName[who]);
1251
1252         if (offset == 0xF000E0)
1253         {
1254                 uint16 data = (tom_jerry_int_pending << 4) | (tom_timer_int_pending << 3)
1255                         | (tom_object_int_pending << 2) | (tom_gpu_int_pending << 1)
1256                         | (tom_video_int_pending << 0);
1257                 //WriteLog("tom: interrupt status is 0x%.4x \n",data);
1258                 return data;
1259         }
1260 //Shoud be handled by the jaguar main loop now... And it is! ;-)
1261 /*      else if (offset == 0xF00006)    // VC
1262         // What if we're in interlaced mode?
1263         // According to docs, in non-interlace mode VC is ALWAYS even...
1264 //              return (tom_scanline << 1);// + 1;
1265 //But it's causing Rayman to be fucked up... Why???
1266 //Because VC is even in NI mode when calling the OP! That's why!
1267                 return (tom_scanline << 1) + 1;//*/
1268 /*
1269 //      F00004          R/W   -----xxx xxxxxxxx   HC - horizontal count
1270 //                            -----x-- --------      (which half of the display)
1271 //                            ------xx xxxxxxxx      (10-bit counter)
1272 */
1273 // This is a kludge to get the HC working somewhat... What we really should do here
1274 // is check what the global time is at the time of the read and calculate the correct HC...
1275 // !!! FIX !!!
1276         else if (offset == 0xF00004)
1277                 return rand() & 0x03FF;
1278         else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE + 0x20))
1279                 return GPUReadWord(offset, who);
1280         else if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE + 0x1000))
1281                 return GPUReadWord(offset, who);
1282 /*      else if ((offset >= 0xF00010) && (offset < 0xF00028))
1283                 return OPReadWord(offset, who);*/
1284         else if ((offset >= 0xF02200) && (offset < 0xF022A0))
1285                 return BlitterReadWord(offset, who);
1286         else if (offset == 0xF00050)
1287                 return tom_timer_prescaler;
1288         else if (offset == 0xF00052)
1289                 return tom_timer_divider;
1290
1291         offset &= 0x3FFF;
1292         return (TOMReadByte(offset, who) << 8) | TOMReadByte(offset + 1, who);
1293 }
1294
1295 //
1296 // TOM byte access (write)
1297 //
1298 void TOMWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
1299 {
1300 //???Is this needed???
1301 // Perhaps on the writes--32-bit writes that is! And masked with FF7FFF...
1302         offset &= 0xFF3FFF;
1303
1304 #ifdef TOM_DEBUG
1305         WriteLog("TOM: Writing byte %02X at %06X\n", data, offset);
1306 #endif
1307
1308         if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
1309         {
1310                 GPUWriteByte(offset, data, who);
1311                 return;
1312         }
1313         else if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
1314         {
1315                 GPUWriteByte(offset, data, who);
1316                 return;
1317         }
1318 /*      else if ((offset >= 0xF00010) && (offset < 0xF00028))
1319         {
1320                 OPWriteByte(offset, data, who);
1321                 return;
1322         }*/
1323         else if ((offset >= 0xF02200) && (offset < 0xF022A0))
1324         {
1325                 BlitterWriteByte(offset, data, who);
1326                 return;
1327         }
1328         else if (offset == 0xF00050)
1329         {
1330                 tom_timer_prescaler = (tom_timer_prescaler & 0x00FF) | (data << 8);
1331                 TOMResetPIT();
1332                 return;
1333         }
1334         else if (offset == 0xF00051)
1335         {
1336                 tom_timer_prescaler = (tom_timer_prescaler & 0xFF00) | data;
1337                 TOMResetPIT();
1338                 return;
1339         }
1340         else if (offset == 0xF00052)
1341         {
1342                 tom_timer_divider = (tom_timer_divider & 0x00FF) | (data << 8);
1343                 TOMResetPIT();
1344                 return;
1345         }
1346         else if (offset == 0xF00053)
1347         {
1348                 tom_timer_divider = (tom_timer_divider & 0xFF00) | data;
1349                 TOMResetPIT();
1350                 return;
1351         }
1352         else if (offset >= 0xF00400 && offset <= 0xF007FF)      // CLUT (A & B)
1353         {
1354                 // Writing to one CLUT writes to the other
1355                 offset &= 0x5FF;                // Mask out $F00600 (restrict to $F00400-5FF)
1356                 tomRam8[offset] = data, tomRam8[offset + 0x200] = data;
1357         }
1358
1359         tomRam8[offset & 0x3FFF] = data;
1360 }
1361
1362 //
1363 // TOM word access (write)
1364 //
1365 void TOMWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
1366 {
1367 //???Is this needed???
1368         offset &= 0xFF3FFF;
1369
1370 #ifdef TOM_DEBUG
1371         WriteLog("TOM: Writing word %04X at %06X\n", data, offset);
1372 #endif
1373 if (offset == 0xF00000 + MEMCON1)
1374         WriteLog("TOM: Memory Configuration 1 written by %s: %04X\n", whoName[who], data);
1375 if (offset == 0xF00000 + MEMCON2)
1376         WriteLog("TOM: Memory Configuration 2 written by %s: %04X\n", whoName[who], data);
1377 if (offset >= 0xF02000 && offset <= 0xF020FF)
1378         WriteLog("TOM: Write attempted to GPU register file by %s (unimplemented)!\n", whoName[who]);
1379
1380         if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
1381         {
1382                 GPUWriteWord(offset, data, who);
1383                 return;
1384         }
1385         else if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
1386         {
1387                 GPUWriteWord(offset, data, who);
1388                 return;
1389         }
1390 //What's so special about this?
1391 /*      else if ((offset >= 0xF00000) && (offset < 0xF00002))
1392         {
1393                 TOMWriteByte(offset, data >> 8);
1394                 TOMWriteByte(offset+1, data & 0xFF);
1395         }*/
1396 /*      else if ((offset >= 0xF00010) && (offset < 0xF00028))
1397         {
1398                 OPWriteWord(offset, data, who);
1399                 return;
1400         }*/
1401         else if (offset == 0xF00050)
1402         {
1403                 tom_timer_prescaler = data;
1404                 TOMResetPIT();
1405                 return;
1406         }
1407         else if (offset == 0xF00052)
1408         {
1409                 tom_timer_divider = data;
1410                 TOMResetPIT();
1411                 return;
1412         }
1413         else if (offset == 0xF000E0)
1414         {
1415 //Check this out...
1416                 if (data & 0x0100)
1417                         tom_video_int_pending = 0;
1418                 if (data & 0x0200)
1419                         tom_gpu_int_pending = 0;
1420                 if (data & 0x0400)
1421                         tom_object_int_pending = 0;
1422                 if (data & 0x0800)
1423                         tom_timer_int_pending = 0;
1424                 if (data & 0x1000)
1425                         tom_jerry_int_pending = 0;
1426         }
1427         else if ((offset >= 0xF02200) && (offset <= 0xF0229F))
1428         {
1429                 BlitterWriteWord(offset, data, who);
1430                 return;
1431         }
1432         else if (offset >= 0xF00400 && offset <= 0xF007FE)      // CLUT (A & B)
1433         {
1434                 // Writing to one CLUT writes to the other
1435                 offset &= 0x5FF;                // Mask out $F00600 (restrict to $F00400-5FF)
1436 // Watch out for unaligned writes here! (Not fixed yet)
1437                 SET16(tomRam8, offset, data);
1438                 SET16(tomRam8, offset + 0x200, data);
1439         }
1440
1441         offset &= 0x3FFF;
1442         if (offset == 0x28)                     // VMODE (Why? Why not OBF?)
1443 //Actually, we should check to see if the Enable bit of VMODE is set before doing this... !!! FIX !!!
1444 #warning "Actually, we should check to see if the Enable bit of VMODE is set before doing this... !!! FIX !!!"
1445                 objectp_running = 1;
1446
1447         if (offset >= 0x30 && offset <= 0x4E)
1448                 data &= 0x07FF;                 // These are (mostly) 11-bit registers
1449         if (offset == 0x2E || offset == 0x36 || offset == 0x54)
1450                 data &= 0x03FF;                 // These are all 10-bit registers
1451
1452         TOMWriteByte(offset, data >> 8, who);
1453         TOMWriteByte(offset+1, data & 0xFF, who);
1454
1455 if (offset == VDB)
1456         WriteLog("TOM: Vertical Display Begin written by %s: %u\n", whoName[who], data);
1457 if (offset == VDE)
1458         WriteLog("TOM: Vertical Display End written by %s: %u\n", whoName[who], data);
1459 if (offset == VP)
1460         WriteLog("TOM: Vertical Period written by %s: %u (%sinterlaced)\n", whoName[who], data, (data & 0x01 ? "non-" : ""));
1461 if (offset == HDB1)
1462         WriteLog("TOM: Horizontal Display Begin 1 written by %s: %u\n", whoName[who], data);
1463 if (offset == HDE)
1464         WriteLog("TOM: Horizontal Display End written by %s: %u\n", whoName[who], data);
1465 if (offset == HP)
1466         WriteLog("TOM: Horizontal Period written by %s: %u (+1*2 = %u)\n", whoName[who], data, (data + 1) * 2);
1467 if (offset == VBB)
1468         WriteLog("TOM: Vertical Blank Begin written by %s: %u\n", whoName[who], data);
1469 if (offset == VBE)
1470         WriteLog("TOM: Vertical Blank End written by %s: %u\n", whoName[who], data);
1471 if (offset == VS)
1472         WriteLog("TOM: Vertical Sync written by %s: %u\n", whoName[who], data);
1473 if (offset == VI)
1474         WriteLog("TOM: Vertical Interrupt written by %s: %u\n", whoName[who], data);
1475 if (offset == HBB)
1476         WriteLog("TOM: Horizontal Blank Begin written by %s: %u\n", whoName[who], data);
1477 if (offset == HBE)
1478         WriteLog("TOM: Horizontal Blank End written by %s: %u\n", whoName[who], data);
1479 if (offset == VMODE)
1480         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));
1481
1482         // detect screen resolution changes
1483 //This may go away in the future, if we do the virtualized screen thing...
1484 //This may go away soon!
1485         if ((offset >= 0x28) && (offset <= 0x4F))
1486         {
1487                 uint32 width = TOMGetVideoModeWidth(), height = TOMGetVideoModeHeight();
1488
1489                 if ((width != tomWidth) || (height != tomHeight))
1490                 {
1491                         tomWidth = width, tomHeight = height;
1492
1493                         if (vjs.renderType == RT_NORMAL)
1494                                 ResizeScreen(tomWidth, tomHeight);
1495                 }
1496         }
1497 }
1498
1499 int TOMIRQEnabled(int irq)
1500 {
1501         // This is the correct byte in big endian... D'oh!
1502 //      return jaguar_byte_read(0xF000E1) & (1 << irq);
1503         return tomRam8[INT1 + 1/*0xE1*/] & (1 << irq);
1504 }
1505
1506 //unused
1507 /*void tom_set_irq_latch(int irq, int enabled)
1508 {
1509         tomRam8[0xE0] = (tomRam8[0xE0] & (~(1<<irq))) | (enabled ? (1<<irq) : 0);
1510 }*/
1511
1512 //unused
1513 /*uint16 tom_irq_control_reg(void)
1514 {
1515         return (tomRam8[0xE0] << 8) | tomRam8[0xE1];
1516 }*/
1517
1518 // NEW:
1519 // TOM Programmable Interrupt Timer handler
1520 // NOTE: TOM's PIT is only enabled if the prescaler is != 0
1521 //       The PIT only generates an interrupt when it counts down to zero, not when loaded!
1522
1523 void TOMPITCallback(void);
1524
1525 void TOMResetPIT(void)
1526 {
1527 #ifndef NEW_TIMER_SYSTEM
1528 //Probably should *add* this amount to the counter to retain cycle accuracy! !!! FIX !!! [DONE]
1529 //Also, why +1??? 'Cause that's what it says in the JTRM...!
1530 //There is a small problem with this approach: If both the prescaler and the divider are equal
1531 //to $FFFF then the counter won't be large enough to handle it. !!! FIX !!!
1532         if (tom_timer_prescaler)
1533                 tom_timer_counter += (1 + tom_timer_prescaler) * (1 + tom_timer_divider);
1534 //      WriteLog("tom: reseting timer to 0x%.8x (%i)\n",tom_timer_counter,tom_timer_counter);
1535 #else
1536         // Need to remove previous timer from the queue, if it exists...
1537         RemoveCallback(TOMPITCallback);
1538
1539         if (tom_timer_prescaler)
1540         {
1541                 double usecs = (float)(tom_timer_prescaler + 1) * (float)(tom_timer_divider + 1) * RISC_CYCLE_IN_USEC;
1542                 SetCallbackTime(TOMPITCallback, usecs);
1543         }
1544 #endif
1545 }
1546
1547 //
1548 // TOM Programmable Interrupt Timer handler
1549 // NOTE: TOM's PIT is only enabled if the prescaler is != 0
1550 //
1551 //NOTE: This is only used by the old execution code... Safe to remove
1552 //      once the timer system is stable.
1553 void TOMExecPIT(uint32 cycles)
1554 {
1555         if (tom_timer_prescaler)
1556         {
1557                 tom_timer_counter -= cycles;
1558
1559                 if (tom_timer_counter <= 0)
1560                 {
1561                         TOMSetPendingTimerInt();
1562                         GPUSetIRQLine(GPUIRQ_TIMER, ASSERT_LINE);       // GPUSetIRQLine does the 'IRQ enabled' checking
1563
1564                         if (TOMIRQEnabled(IRQ_TIMER))
1565                                 m68k_set_irq(7);                                // Cause a 68000 NMI...
1566
1567                         TOMResetPIT();
1568                 }
1569         }
1570 }
1571
1572
1573 void TOMPITCallback(void)
1574 {
1575 //      INT1_RREG |= 0x08;                         // Set TOM PIT interrupt pending
1576         TOMSetPendingTimerInt();
1577     GPUSetIRQLine(GPUIRQ_TIMER, ASSERT_LINE);  // It does the 'IRQ enabled' checking
1578
1579 //      if (INT1_WREG & 0x08)
1580         if (TOMIRQEnabled(IRQ_TIMER))
1581                 m68k_set_irq(7);                       // Generate 68K NMI
1582
1583         TOMResetPIT();
1584 }