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