-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgpu.c
More file actions
1819 lines (1561 loc) · 46.1 KB
/
gpu.c
File metadata and controls
1819 lines (1561 loc) · 46.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// GPU Core
//
// Originally by David Raingeard (Cal2)
// GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
// Cleanups, endian wrongness, and bad ASM amelioration by James Hammons
// (C) 2010 Underground Software
//
// JLH = James Hammons <[email protected]>
//
// Who When What
// --- ---------- -------------------------------------------------------------
// JLH 01/16/2010 Created this log ;-)
// JLH 11/26/2011 Added fixes for LOAD/STORE alignment issues
//
// Note: Endian wrongness probably stems from the MAME origins of this emu and
// the braindead way in which MAME handles memory. :-)
//
// Problem with not booting the BIOS was the incorrect way that the
// SUBC instruction set the carry when the carry was set going in...
// Same problem with ADDC...
//
#include "gpu.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // For memset
#include "log.h"
#include "dsp.h"
#include "jaguar.h"
#include "m68000/m68kinterface.h"
#include "tom.h"
// Seems alignment in loads & stores was off...
#define GPU_CORRECT_ALIGNMENT
#define GPU_TRACE_DEBUG 0
#if GPU_TRACE_DEBUG
#define GPU_TRACE(...) LOG_DBG("[GPU-TRACE] " __VA_ARGS__)
#else
#define GPU_TRACE(...) do {} while(0)
#endif
// For GPU dissasembly...
// Various bits
#define CINT0FLAG 0x0200
#define CINT1FLAG 0x0400
#define CINT2FLAG 0x0800
#define CINT3FLAG 0x1000
#define CINT4FLAG 0x2000
#define CINT04FLAGS (CINT0FLAG | CINT1FLAG | CINT2FLAG | CINT3FLAG | CINT4FLAG)
// GPU_FLAGS bits
#define ZERO_FLAG 0x0001
#define CARRY_FLAG 0x0002
#define NEGA_FLAG 0x0004
#define IMASK 0x0008
#define INT_ENA0 0x0010
#define INT_ENA1 0x0020
#define INT_ENA2 0x0040
#define INT_ENA3 0x0080
#define INT_ENA4 0x0100
#define INT_CLR0 0x0200
#define INT_CLR1 0x0400
#define INT_CLR2 0x0800
#define INT_CLR3 0x1000
#define INT_CLR4 0x2000
#define REGPAGE 0x4000
#define DMAEN 0x8000
// Private function prototypes
void GPUUpdateRegisterBanks(void);
INLINE static void gpu_opcode_add(void);
INLINE static void gpu_opcode_addc(void);
INLINE static void gpu_opcode_addq(void);
INLINE static void gpu_opcode_addqt(void);
INLINE static void gpu_opcode_sub(void);
INLINE static void gpu_opcode_subc(void);
INLINE static void gpu_opcode_subq(void);
INLINE static void gpu_opcode_subqt(void);
INLINE static void gpu_opcode_neg(void);
INLINE static void gpu_opcode_and(void);
INLINE static void gpu_opcode_or(void);
INLINE static void gpu_opcode_xor(void);
INLINE static void gpu_opcode_not(void);
INLINE static void gpu_opcode_btst(void);
INLINE static void gpu_opcode_bset(void);
INLINE static void gpu_opcode_bclr(void);
INLINE static void gpu_opcode_mult(void);
INLINE static void gpu_opcode_imult(void);
INLINE static void gpu_opcode_imultn(void);
INLINE static void gpu_opcode_resmac(void);
INLINE static void gpu_opcode_imacn(void);
INLINE static void gpu_opcode_div(void);
INLINE static void gpu_opcode_abs(void);
INLINE static void gpu_opcode_sh(void);
INLINE static void gpu_opcode_shlq(void);
INLINE static void gpu_opcode_shrq(void);
INLINE static void gpu_opcode_sha(void);
INLINE static void gpu_opcode_sharq(void);
INLINE static void gpu_opcode_ror(void);
INLINE static void gpu_opcode_rorq(void);
INLINE static void gpu_opcode_cmp(void);
INLINE static void gpu_opcode_cmpq(void);
INLINE static void gpu_opcode_sat8(void);
INLINE static void gpu_opcode_sat16(void);
INLINE static void gpu_opcode_move(void);
INLINE static void gpu_opcode_moveq(void);
INLINE static void gpu_opcode_moveta(void);
INLINE static void gpu_opcode_movefa(void);
INLINE static void gpu_opcode_movei(void);
INLINE static void gpu_opcode_loadb(void);
INLINE static void gpu_opcode_loadw(void);
INLINE static void gpu_opcode_load(void);
INLINE static void gpu_opcode_loadp(void);
INLINE static void gpu_opcode_load_r14_indexed(void);
INLINE static void gpu_opcode_load_r15_indexed(void);
INLINE static void gpu_opcode_storeb(void);
INLINE static void gpu_opcode_storew(void);
INLINE static void gpu_opcode_store(void);
INLINE static void gpu_opcode_storep(void);
INLINE static void gpu_opcode_store_r14_indexed(void);
INLINE static void gpu_opcode_store_r15_indexed(void);
INLINE static void gpu_opcode_move_pc(void);
INLINE static void gpu_opcode_jump(void);
INLINE static void gpu_opcode_jr(void);
INLINE static void gpu_opcode_mmult(void);
INLINE static void gpu_opcode_mtoi(void);
INLINE static void gpu_opcode_normi(void);
INLINE static void gpu_opcode_nop(void);
INLINE static void gpu_opcode_load_r14_ri(void);
INLINE static void gpu_opcode_load_r15_ri(void);
INLINE static void gpu_opcode_store_r14_ri(void);
INLINE static void gpu_opcode_store_r15_ri(void);
INLINE static void gpu_opcode_sat24(void);
INLINE static void gpu_opcode_pack(void);
INLINE static void executeOpcode(uint32_t index);
uint8_t gpu_opcode_cycles[64] =
{
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1
};
void (*gpu_opcode[64])()=
{
gpu_opcode_add, gpu_opcode_addc, gpu_opcode_addq, gpu_opcode_addqt,
gpu_opcode_sub, gpu_opcode_subc, gpu_opcode_subq, gpu_opcode_subqt,
gpu_opcode_neg, gpu_opcode_and, gpu_opcode_or, gpu_opcode_xor,
gpu_opcode_not, gpu_opcode_btst, gpu_opcode_bset, gpu_opcode_bclr,
gpu_opcode_mult, gpu_opcode_imult, gpu_opcode_imultn, gpu_opcode_resmac,
gpu_opcode_imacn, gpu_opcode_div, gpu_opcode_abs, gpu_opcode_sh,
gpu_opcode_shlq, gpu_opcode_shrq, gpu_opcode_sha, gpu_opcode_sharq,
gpu_opcode_ror, gpu_opcode_rorq, gpu_opcode_cmp, gpu_opcode_cmpq,
gpu_opcode_sat8, gpu_opcode_sat16, gpu_opcode_move, gpu_opcode_moveq,
gpu_opcode_moveta, gpu_opcode_movefa, gpu_opcode_movei, gpu_opcode_loadb,
gpu_opcode_loadw, gpu_opcode_load, gpu_opcode_loadp, gpu_opcode_load_r14_indexed,
gpu_opcode_load_r15_indexed, gpu_opcode_storeb, gpu_opcode_storew, gpu_opcode_store,
gpu_opcode_storep, gpu_opcode_store_r14_indexed, gpu_opcode_store_r15_indexed, gpu_opcode_move_pc,
gpu_opcode_jump, gpu_opcode_jr, gpu_opcode_mmult, gpu_opcode_mtoi,
gpu_opcode_normi, gpu_opcode_nop, gpu_opcode_load_r14_ri, gpu_opcode_load_r15_ri,
gpu_opcode_store_r14_ri, gpu_opcode_store_r15_ri, gpu_opcode_sat24, gpu_opcode_pack,
};
static uint8_t gpu_ram_8[0x1000];
uint32_t gpu_pc;
static uint32_t gpu_acc;
static uint32_t gpu_remain;
static uint32_t gpu_hidata;
static uint32_t gpu_flags;
static uint32_t gpu_matrix_control;
static uint32_t gpu_pointer_to_matrix;
static uint32_t gpu_data_organization;
static uint32_t gpu_control;
static uint32_t gpu_div_control;
// There is a distinct advantage to having these separated out--there's no need to clear
// a bit before writing a result. I.e., if the result of an operation leaves a zero in
// the carry flag, you don't have to zero gpu_flag_c before you can write that zero!
static uint8_t gpu_flag_z, gpu_flag_n, gpu_flag_c;
uint32_t gpu_reg_bank_0[32];
uint32_t gpu_reg_bank_1[32];
static uint32_t * gpu_reg;
static uint32_t * gpu_alternate_reg;
static uint32_t gpu_instruction;
static uint32_t gpu_opcode_first_parameter;
static uint32_t gpu_opcode_second_parameter;
#define GPU_RUNNING (gpu_control & 0x01)
#define RM gpu_reg[gpu_opcode_first_parameter]
#define RN gpu_reg[gpu_opcode_second_parameter]
#define ALTERNATE_RM gpu_alternate_reg[gpu_opcode_first_parameter]
#define ALTERNATE_RN gpu_alternate_reg[gpu_opcode_second_parameter]
#define IMM_1 gpu_opcode_first_parameter
#define IMM_2 gpu_opcode_second_parameter
#define SET_FLAG_Z(r) (gpu_flag_z = ((r) == 0));
#define SET_FLAG_N(r) (gpu_flag_n = (((uint32_t)(r) >> 31) & 0x01));
#define RESET_FLAG_Z() gpu_flag_z = 0;
#define RESET_FLAG_N() gpu_flag_n = 0;
#define RESET_FLAG_C() gpu_flag_c = 0;
#define CLR_Z (gpu_flag_z = 0)
#define CLR_ZN (gpu_flag_z = gpu_flag_n = 0)
#define CLR_ZNC (gpu_flag_z = gpu_flag_n = gpu_flag_c = 0)
#define SET_Z(r) (gpu_flag_z = ((r) == 0))
#define SET_N(r) (gpu_flag_n = (((uint32_t)(r) >> 31) & 0x01))
#define SET_C_ADD(a,b) (gpu_flag_c = ((uint32_t)(b) > (uint32_t)(~(a))))
#define SET_C_SUB(a,b) (gpu_flag_c = ((uint32_t)(b) > (uint32_t)(a)))
#define SET_ZN(r) SET_N(r); SET_Z(r)
#define SET_ZNC_ADD(a,b,r) SET_N(r); SET_Z(r); SET_C_ADD(a,b)
#define SET_ZNC_SUB(a,b,r) SET_N(r); SET_Z(r); SET_C_SUB(a,b)
uint32_t gpu_convert_zero[32] =
{ 32,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 };
uint8_t * branch_condition_table = 0;
#define BRANCH_CONDITION(x) branch_condition_table[(x) + ((jaguar_flags & 7) << 5)]
static uint32_t gpu_in_exec = 0;
static uint32_t gpu_releaseTimeSlice_flag = 0;
void GPUReleaseTimeslice(void)
{
gpu_releaseTimeSlice_flag = 1;
}
uint32_t GPUGetPC(void)
{
return gpu_pc;
}
void build_branch_condition_table(void)
{
unsigned i, j;
if (branch_condition_table)
return;
branch_condition_table = (uint8_t *)malloc(32 * 8 * sizeof(branch_condition_table[0]));
if (!branch_condition_table)
return;
for(i=0; i<8; i++)
{
for(j=0; j<32; j++)
{
int result = 1;
if (j & 1)
if (i & ZERO_FLAG)
result = 0;
if (j & 2)
if (!(i & ZERO_FLAG))
result = 0;
if (j & 4)
if (i & (CARRY_FLAG << (j >> 4)))
result = 0;
if (j & 8)
if (!(i & (CARRY_FLAG << (j >> 4))))
result = 0;
branch_condition_table[i * 32 + j] = result;
}
}
}
// GPU byte access (read)
uint8_t GPUReadByte(uint32_t offset, uint32_t who/*=UNKNOWN*/)
{
if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
return gpu_ram_8[offset & 0xFFF];
else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
{
uint32_t data = GPUReadLong(offset & 0xFFFFFFFC, who);
if ((offset & 0x03) == 0)
return data >> 24;
else if ((offset & 0x03) == 1)
return (data >> 16) & 0xFF;
else if ((offset & 0x03) == 2)
return (data >> 8) & 0xFF;
else if ((offset & 0x03) == 3)
return data & 0xFF;
}
return JaguarReadByte(offset, who);
}
// GPU word access (read)
uint16_t GPUReadWord(uint32_t offset, uint32_t who/*=UNKNOWN*/)
{
if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
{
uint16_t data;
offset &= 0xFFF;
data = ((uint16_t)gpu_ram_8[offset] << 8) | (uint16_t)gpu_ram_8[offset+1];
return data;
}
else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
{
uint32_t data;
// This looks and smells wrong...
// But it *might* be OK...
if (offset & 0x01) // Catch cases 1 & 3... (unaligned read)
return (GPUReadByte(offset, who) << 8) | GPUReadByte(offset+1, who);
data = GPUReadLong(offset & 0xFFFFFFFC, who);
if (offset & 0x02) // Cases 0 & 2...
return data & 0xFFFF;
return data >> 16;
}
return JaguarReadWord(offset, who);
}
// GPU dword access (read)
uint32_t GPUReadLong(uint32_t offset, uint32_t who/*=UNKNOWN*/)
{
if (offset >= 0xF02000 && offset <= 0xF020FF)
{
uint32_t reg = (offset & 0xFC) >> 2;
return (reg < 32 ? gpu_reg_bank_0[reg] : gpu_reg_bank_1[reg - 32]);
}
if ((offset >= GPU_WORK_RAM_BASE) && (offset <= GPU_WORK_RAM_BASE + 0x0FFC))
{
offset &= 0xFFF;
return ((uint32_t)gpu_ram_8[offset] << 24) | ((uint32_t)gpu_ram_8[offset+1] << 16)
| ((uint32_t)gpu_ram_8[offset+2] << 8) | (uint32_t)gpu_ram_8[offset+3];//*/
}
else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset <= GPU_CONTROL_RAM_BASE + 0x1C))
{
offset &= 0x1F;
switch (offset)
{
case 0x00:
gpu_flag_c = (gpu_flag_c ? 1 : 0);
gpu_flag_z = (gpu_flag_z ? 1 : 0);
gpu_flag_n = (gpu_flag_n ? 1 : 0);
gpu_flags = (gpu_flags & 0xFFFFFFF8) | (gpu_flag_n << 2) | (gpu_flag_c << 1) | gpu_flag_z;
return gpu_flags & 0xFFFFC1FF;
case 0x04:
return gpu_matrix_control;
case 0x08:
return gpu_pointer_to_matrix;
case 0x0C:
return gpu_data_organization;
case 0x10:
return gpu_pc;
case 0x14:
return gpu_control;
case 0x18:
return gpu_hidata;
case 0x1C:
return gpu_remain;
default: // unaligned long read
break;
}
return 0;
}
return (JaguarReadWord(offset, who) << 16) | JaguarReadWord(offset + 2, who);
}
// GPU byte access (write)
void GPUWriteByte(uint32_t offset, uint8_t data, uint32_t who/*=UNKNOWN*/)
{
if ((offset >= GPU_WORK_RAM_BASE) && (offset <= GPU_WORK_RAM_BASE + 0x0FFF))
{
gpu_ram_8[offset & 0xFFF] = data;
return;
}
else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset <= GPU_CONTROL_RAM_BASE + 0x1F))
{
uint32_t reg = offset & 0x1C;
int bytenum = offset & 0x03;
//This is definitely wrong!
if ((reg >= 0x1C) && (reg <= 0x1F))
gpu_div_control = (gpu_div_control & (~(0xFF << (bytenum << 3)))) | (data << (bytenum << 3));
else
{
uint32_t old_data = GPUReadLong(offset & 0xFFFFFFC, who);
bytenum = 3 - bytenum; // convention motorola !!!
old_data = (old_data & (~(0xFF << (bytenum << 3)))) | (data << (bytenum << 3));
GPUWriteLong(offset & 0xFFFFFFC, old_data, who);
}
return;
}
JaguarWriteByte(offset, data, who);
}
// GPU word access (write)
void GPUWriteWord(uint32_t offset, uint16_t data, uint32_t who/*=UNKNOWN*/)
{
if ((offset >= GPU_WORK_RAM_BASE) && (offset <= GPU_WORK_RAM_BASE + 0x0FFE))
{
gpu_ram_8[offset & 0xFFF] = (data>>8) & 0xFF;
gpu_ram_8[(offset+1) & 0xFFF] = data & 0xFF;//*/
return;
}
else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset <= GPU_CONTROL_RAM_BASE + 0x1E))
{
if (offset & 0x01) // This is supposed to weed out unaligned writes, but does nothing...
return;
//Dual locations in this range: $1C Divide unit remainder/Divide unit control (R/W)
//This just literally sucks.
if ((offset & 0x1C) == 0x1C)
{
//This doesn't look right either--handles cases 1, 2, & 3 all the same!
if (offset & 0x02)
gpu_div_control = (gpu_div_control & 0xFFFF0000) | (data & 0xFFFF);
else
gpu_div_control = (gpu_div_control & 0x0000FFFF) | ((data & 0xFFFF) << 16);
}
else
{
uint32_t old_data = GPUReadLong(offset & 0xFFFFFFC, who);
if (offset & 0x02)
old_data = (old_data & 0xFFFF0000) | (data & 0xFFFF);
else
old_data = (old_data & 0x0000FFFF) | ((data & 0xFFFF) << 16);
GPUWriteLong(offset & 0xFFFFFFC, old_data, who);
}
return;
}
else if ((offset == GPU_WORK_RAM_BASE + 0x0FFF) || (offset == GPU_CONTROL_RAM_BASE + 0x1F))
return;
// Have to be careful here--this can cause an infinite loop!
JaguarWriteWord(offset, data, who);
}
// GPU dword access (write)
void GPUWriteLong(uint32_t offset, uint32_t data, uint32_t who/*=UNKNOWN*/)
{
if ((offset >= GPU_WORK_RAM_BASE) && (offset <= GPU_WORK_RAM_BASE + 0x0FFC))
{
offset &= 0xFFF;
SET32(gpu_ram_8, offset, data);
return;
}
else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset <= GPU_CONTROL_RAM_BASE + 0x1C))
{
offset &= 0x1F;
switch (offset)
{
case 0x00:
{
bool wasIMASK = (gpu_flags & IMASK) ? 1 : 0;
bool IMASKCleared = wasIMASK && !(data & IMASK);
gpu_flags = (data & ~IMASK) | ((data & IMASK) ? (gpu_flags & IMASK) : 0);
gpu_flag_z = gpu_flags & ZERO_FLAG;
gpu_flag_c = (gpu_flags & CARRY_FLAG) >> 1;
gpu_flag_n = (gpu_flags & NEGA_FLAG) >> 2;
GPUUpdateRegisterBanks();
gpu_control &= ~((gpu_flags & CINT04FLAGS) >> 3); // Interrupt latch clear bits
//Writing here is only an interrupt enable--this approach is just plain wrong!
// GPUHandleIRQs();
//This, however, is A-OK! ;-)
if (IMASKCleared) // If IMASK was cleared,
GPUHandleIRQs(); // see if any other interrupts need servicing!
break;
}
case 0x04:
gpu_matrix_control = data;
break;
case 0x08:
// This can only point to long aligned addresses
gpu_pointer_to_matrix = data & 0xFFFFFFFC;
break;
case 0x0C:
gpu_data_organization = data;
break;
case 0x10:
gpu_pc = data;
break;
case 0x14:
{
data &= ~0xF7C0; // Disable writes to INT_LAT0-4 & TOM version number
// check for GPU -> CPU interrupt
if (data & 0x02)
{
if (TOMIRQEnabled(IRQ_GPU))
{
//This is the programmer's responsibility, to make sure the handler is valid, not ours!
// if ((TOMIRQEnabled(IRQ_GPU))// && (JaguarInterruptHandlerIsValid(64)))
{
TOMSetPendingGPUInt();
m68k_set_irq(2); // Set 68000 IPL 2
GPUReleaseTimeslice();
}
}
data &= ~0x02;
}
// check for CPU -> GPU interrupt #0
if (data & 0x04)
{
GPUSetIRQLine(0, ASSERT_LINE);
m68k_end_timeslice();
DSPReleaseTimeslice();
data &= ~0x04;
}
gpu_control = (gpu_control & 0xF7C0) | (data & (~0xF7C0));
// if gpu wasn't running but is now running, execute a few cycles
#ifdef GPU_SINGLE_STEPPING
if (gpu_control & 0x18)
GPUExec(1);
#endif
// (?) If we're set running by the M68K (or DSP?) then end its timeslice to
// allow the GPU a chance to run...
// Yes! This partially fixed Trevor McFur...
if (GPU_RUNNING)
m68k_end_timeslice();
break;
}
case 0x18:
gpu_hidata = data;
break;
case 0x1C:
gpu_div_control = data;
break;
// default: // unaligned long write
//exit(0);
//__asm int 3
}
return;
}
// JaguarWriteWord(offset, (data >> 16) & 0xFFFF, who);
// JaguarWriteWord(offset+2, data & 0xFFFF, who);
// We're a 32-bit processor, we can do a long write...!
JaguarWriteLong(offset, data, who);
}
// Change register banks if necessary
void GPUUpdateRegisterBanks(void)
{
int bank = (gpu_flags & REGPAGE); // REGPAGE bit
if (gpu_flags & IMASK) // IMASK bit
bank = 0; // IMASK forces main bank to be bank 0
if (bank)
gpu_reg = gpu_reg_bank_1, gpu_alternate_reg = gpu_reg_bank_0;
else
gpu_reg = gpu_reg_bank_0, gpu_alternate_reg = gpu_reg_bank_1;
}
void GPUHandleIRQs(void)
{
uint32_t bits, mask;
uint32_t which = 0; //Isn't there a #pragma to disable this warning???
// Bail out if we're already in an interrupt!
if (gpu_flags & IMASK)
return;
// Get the interrupt latch & enable bits
bits = (gpu_control >> 6) & 0x1F;
mask = (gpu_flags >> 4) & 0x1F;
// Bail out if latched interrupts aren't enabled
bits &= mask;
if (!bits)
return;
// Determine which interrupt to service
if (bits & 0x01)
which = 0;
if (bits & 0x02)
which = 1;
if (bits & 0x04)
which = 2;
if (bits & 0x08)
which = 3;
if (bits & 0x10)
which = 4;
// set the interrupt flag
gpu_flags |= IMASK;
GPUUpdateRegisterBanks();
// subqt #4,r31 ; pre-decrement stack pointer
// move pc,r30 ; address of interrupted code
// store r30,(r31) ; store return address
gpu_reg[31] -= 4;
GPUWriteLong(gpu_reg[31], gpu_pc - 2, GPU);
// movei #service_address,r30 ; pointer to ISR entry
// jump (r30) ; jump to ISR
// nop
gpu_pc = gpu_reg[30] = GPU_WORK_RAM_BASE + (which * 0x10);
}
void GPUSetIRQLine(int irqline, int state)
{
uint32_t mask = 0x0040 << irqline;
gpu_control &= ~mask; // Clear the interrupt latch
if (state)
{
gpu_control |= mask; // Assert the interrupt latch
GPUHandleIRQs(); // And handle the interrupt...
}
}
void GPUInit(void)
{
build_branch_condition_table();
GPUReset();
}
void GPUDone(void)
{
/* Release the branch-condition LUT so process-lifetime ASAN runs
* don't report it as a leak. Safe to call without a matching
* GPUInit (free(NULL) is a no-op) and safe to re-call after a
* subsequent GPUInit (build_branch_condition_table early-outs on
* non-NULL pointer). */
if (branch_condition_table)
{
free(branch_condition_table);
branch_condition_table = NULL;
}
}
void GPUReset(void)
{
unsigned i;
// GPU registers (directly visible)
gpu_flags = 0x00000000;
gpu_matrix_control = 0x00000000;
gpu_pointer_to_matrix = 0x00000000;
gpu_data_organization = 0xFFFFFFFF;
gpu_pc = 0x00F03000;
gpu_control = 0x00002800; // Correctly sets this as TOM Rev. 2
gpu_hidata = 0x00000000;
gpu_remain = 0x00000000; // These two registers are RO/WO
gpu_div_control = 0x00000000;
// GPU internal register
gpu_acc = 0x00000000;
gpu_reg = gpu_reg_bank_0;
gpu_alternate_reg = gpu_reg_bank_1;
for(i=0; i<32; i++)
gpu_reg[i] = gpu_alternate_reg[i] = 0x00000000;
CLR_ZNC;
memset(gpu_ram_8, 0xFF, 0x1000);
gpu_in_exec = 0;
//not needed GPUInterruptPending = false;
GPUResetStats();
// Contents of local RAM are quasi-stable; we simulate this by randomizing RAM contents
for(i=0; i<4096; i+=4)
*((uint32_t *)(&gpu_ram_8[i])) = JaguarRand();
}
uint32_t GPUReadPC(void)
{
return gpu_pc;
}
void GPUResetStats(void)
{
}
int GPUIsRunning(void)
{
return GPU_RUNNING ? 1 : 0;
}
void GPUDumpState(const char *tag)
{
LOG_INF("[GPU %s] PC=%08X ctrl=%08X flags=%08X running=%d\n",
tag ? tag : "", gpu_pc, gpu_control, gpu_flags, GPU_RUNNING ? 1 : 0);
}
// Main GPU execution core
void GPUExec(int32_t cycles)
{
if (!GPU_RUNNING)
return;
#ifdef GPU_SINGLE_STEPPING
if (gpu_control & 0x18)
{
cycles = 1;
gpu_control &= ~0x10;
}
#endif
GPUHandleIRQs();
gpu_releaseTimeSlice_flag = 0;
gpu_in_exec++;
while (cycles > 0 && GPU_RUNNING)
{
uint16_t opcode;
uint32_t index;
if (gpu_pc >= GPU_WORK_RAM_BASE && gpu_pc < GPU_WORK_RAM_BASE + 0x1000)
{
uint32_t off = gpu_pc - GPU_WORK_RAM_BASE;
opcode = ((uint16_t)gpu_ram_8[off] << 8) | (uint16_t)gpu_ram_8[off + 1];
}
else
opcode = GPUReadWord(gpu_pc, GPU);
index = opcode >> 10;
gpu_instruction = opcode; // Added for GPU #3...
gpu_opcode_first_parameter = (opcode >> 5) & 0x1F;
gpu_opcode_second_parameter = opcode & 0x1F;
//$E400 -> 1110 01 -> $39 -> 57
//GPU #1
gpu_pc += 2;
#if 0
gpu_opcode[index]();
#else
executeOpcode(index);
#endif
// BIOS hacking
//GPU: [00F03548] jr nz,00F03560 (0xd561) (RM=00F03114, RN=00000004) -> --> JR: Branch taken.
//GPU: [00F0354C] jump nz,(r29) (0xd3a1) (RM=00F03314, RN=00000004) -> (RM=00F03314, RN=00000004)
cycles -= gpu_opcode_cycles[index];
}
gpu_in_exec--;
}
INLINE static void executeOpcode(uint32_t index) {
switch (index) {
case 0:
gpu_opcode_add();
break;
case 1:
gpu_opcode_addc();
break;
case 2:
gpu_opcode_addq();
break;
case 3:
gpu_opcode_addqt();
break;
case 4:
gpu_opcode_sub();
break;
case 5:
gpu_opcode_subc();
break;
case 6:
gpu_opcode_subq();
break;
case 7:
gpu_opcode_subqt();
break;
case 8:
gpu_opcode_neg();
break;
case 9:
gpu_opcode_and();
break;
case 10:
gpu_opcode_or();
break;
case 11:
gpu_opcode_xor();
break;
case 12:
gpu_opcode_not();
break;
case 13:
gpu_opcode_btst();
break;
case 14:
gpu_opcode_bset();
break;
case 15:
gpu_opcode_bclr();
break;
case 16:
gpu_opcode_mult();
break;
case 17:
gpu_opcode_imult();
break;
case 18:
gpu_opcode_imultn();
break;
case 19:
gpu_opcode_resmac();
break;
case 20:
gpu_opcode_imacn();
break;
case 21:
gpu_opcode_div();
break;
case 22:
gpu_opcode_abs();
break;
case 23:
gpu_opcode_sh();
break;
case 24:
gpu_opcode_shlq();
break;
case 25:
gpu_opcode_shrq();
break;
case 26:
gpu_opcode_sha();
break;
case 27:
gpu_opcode_sharq();
break;
case 28:
gpu_opcode_ror();
break;
case 29:
gpu_opcode_rorq();
break;
case 30:
gpu_opcode_cmp();
break;
case 31:
gpu_opcode_cmpq();
break;
case 32:
gpu_opcode_sat8();
break;
case 33:
gpu_opcode_sat16();
break;
case 34:
gpu_opcode_move();
break;
case 35:
gpu_opcode_moveq();
break;
case 36:
gpu_opcode_moveta();
break;
case 37:
gpu_opcode_movefa();
break;
case 38:
gpu_opcode_movei();
break;
case 39:
gpu_opcode_loadb();
break;
case 40:
gpu_opcode_loadw();
break;
case 41:
gpu_opcode_load();
break;
case 42:
gpu_opcode_loadp();
break;
case 43:
gpu_opcode_load_r14_indexed();
break;
case 44:
gpu_opcode_load_r15_indexed();
break;
case 45:
gpu_opcode_storeb();
break;
case 46:
gpu_opcode_storew();
break;
case 47:
gpu_opcode_store();
break;
case 48:
gpu_opcode_storep();
break;
case 49:
gpu_opcode_store_r14_indexed();
break;
case 50:
gpu_opcode_store_r15_indexed();
break;
case 51:
gpu_opcode_move_pc();
break;
case 52:
gpu_opcode_jump();
break;
case 53:
gpu_opcode_jr();
break;
case 54:
gpu_opcode_mmult();
break;
case 55:
gpu_opcode_mtoi();
break;
case 56:
gpu_opcode_normi();
break;
case 57:
gpu_opcode_nop();
break;
case 58:
gpu_opcode_load_r14_ri();
break;
case 59:
gpu_opcode_load_r15_ri();
break;
case 60:
gpu_opcode_store_r14_ri();
break;
case 61:
gpu_opcode_store_r15_ri();
break;
case 62:
gpu_opcode_sat24();
break;
case 63:
gpu_opcode_pack();
break;
default:
// WriteLog("\nUnknown opcode %i\n", index);
break;
}
}
// GPU opcodes
/*
GPU opcodes use (offset punch--vertically below bad guy):
add 18686
addq 32621
sub 7483
subq 10252
and 21229
or 15003
btst 1822
bset 2072
mult 141
div 2392
shlq 13449
shrq 10297
sharq 11104
cmp 6775
cmpq 5944
move 31259
moveq 4473
movei 23277
loadb 46
loadw 4201
load 28580
load_r14_indexed 1183
load_r15_indexed 1125
storew 178
store 10144
store_r14_indexed 320
store_r15_indexed 1
move_pc 1742
jump 24467
jr 18090
nop 41362