-
Notifications
You must be signed in to change notification settings - Fork 21
/
mod.rs
1936 lines (1601 loc) · 65.8 KB
/
mod.rs
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
use rustc_serialize::{Decodable, Encodable, Decoder, Encoder};
use memory::Addressable;
use memory::timers::Timers;
use shared::SharedState;
use interrupt::Interrupt;
use timekeeper::{Peripheral, Cycles, FracCycles};
use self::renderer::{Renderer, Vertex, PrimitiveAttributes};
use self::renderer::{BlendMode, SemiTransparencyMode, TextureDepth};
pub mod renderer;
#[derive(RustcDecodable, RustcEncodable)]
pub struct Gpu {
/// Draw mode for rectangles, dithering enable and a few other
/// things
draw_mode: u16,
/// Texture window x mask (8 pixel steps)
texture_window_x_mask: u8,
/// Texture window y mask (8 pixel steps)
texture_window_y_mask: u8,
/// Texture window x offset (8 pixel steps)
texture_window_x_offset: u8,
/// Texture window y offset (8 pixel steps)
texture_window_y_offset: u8,
/// Enable dithering from 24 to 15bits RGB
dithering: bool,
/// Allow drawing to the display area
draw_to_display: bool,
/// Force "mask" bit of the pixel to 1 when writing to VRAM
/// (otherwise don't modify it)
force_set_mask_bit: bool,
/// Don't draw to pixels which have the "mask" bit set
preserve_masked_pixels: bool,
/// Left-most column of drawing area
drawing_area_left: u16,
/// Top-most line of drawing area
drawing_area_top: u16,
/// Right-most column of drawing area
drawing_area_right: u16,
/// Bottom-most line of drawing area
drawing_area_bottom: u16,
/// Drawing offset in the framebuffer
drawing_offset: (i16, i16),
/// Currently displayed field. For progressive output this is
/// always Top.
field: Field,
/// When true all textures are disabled
texture_disable: bool,
/// Video output horizontal resolution
hres: HorizontalRes,
/// Video output vertical resolution
vres: VerticalRes,
/// Video mode
vmode: VMode,
/// Display depth. The GPU itself always draws 15bit RGB, 24bit
/// output must use external assets (pre-rendered textures, MDEC,
/// etc...)
display_depth: DisplayDepth,
/// Output interlaced video signal instead of progressive
interlaced: bool,
/// Disable the display
display_disabled: bool,
/// First column of the display area in VRAM
display_vram_x_start: u16,
/// First line of the display area in VRAM
display_vram_y_start: u16,
/// Display output horizontal start relative to HSYNC
display_horiz_start: u16,
/// Display output horizontal end relative to HSYNC
display_horiz_end: u16,
/// Display output first line relative to VSYNC
display_line_start: u16,
/// Display output last line relative to VSYNC
display_line_end: u16,
/// DMA request direction
dma_direction: DmaDirection,
/// Handler function for GP0 writes
gp0_handler: Gp0Handler,
/// Buffer containing the current GP0 command
gp0_command: CommandBuffer,
/// Remaining number of words to fetch for the current GP0 command
gp0_words_remaining: u32,
/// Current GP0 command attributes
gp0_attributes: Gp0Attributes,
/// True when the GP0 interrupt has been requested
gp0_interrupt: bool,
/// True when the VBLANK interrupt is high
vblank_interrupt: bool,
/// Fractional GPU cycle remainder resulting from the CPU
/// clock/GPU clock time conversion. Effectively the phase of the
/// GPU clock relative to the CPU, expressed in CPU clock periods.
gpu_clock_phase: u16,
/// Currently displayed video output line
display_line: u16,
/// Current GPU clock tick for the current line
display_line_tick: u16,
/// Video standard (PAL or NTSC)
standard: VideoClock,
/// Next word returned by the GPUREAD command
read_word: u32,
/// When drawing polylines we must keep track of the previous
/// vertex position and color
polyline_prev: ([i16; 2], [u8; 3]),
/// Image buffer for texture uploads
load_buffer: ImageBuffer,
}
impl Gpu {
pub fn new(standard: VideoClock) -> Gpu {
let dummy_gp0 =
Gp0Attributes::new(Gpu::gp0_nop, false, BlendMode::None, false);
Gpu {
//renderer: renderer,
draw_mode: 0,
texture_window_x_mask: 0,
texture_window_y_mask: 0,
texture_window_x_offset: 0,
texture_window_y_offset: 0,
dithering: false,
draw_to_display: false,
force_set_mask_bit: false,
preserve_masked_pixels: false,
drawing_area_left: 0,
drawing_area_top: 0,
drawing_area_right: 0,
drawing_area_bottom: 0,
drawing_offset: (0, 0),
field: Field::Top,
texture_disable: false,
hres: HorizontalRes::from_fields(0, 0),
vres: VerticalRes::Y240Lines,
vmode: VMode::Ntsc,
display_depth: DisplayDepth::D15Bits,
interlaced: false,
display_disabled: true,
display_vram_x_start: 0,
display_vram_y_start: 0,
display_horiz_start: 0x200,
display_horiz_end: 0xc00,
display_line_start: 0x10,
display_line_end: 0x100,
dma_direction: DmaDirection::Off,
gp0_handler: Gp0Handler(Gpu::gp0_handle_command),
gp0_command: CommandBuffer::new(),
gp0_words_remaining: 0,
gp0_attributes: dummy_gp0,
gp0_interrupt: false,
vblank_interrupt: false,
gpu_clock_phase: 0,
display_line: 0,
display_line_tick: 0,
standard: standard,
read_word: 0,
polyline_prev: ([0; 2], [0; 3]),
load_buffer: ImageBuffer::new(),
}
}
/// Return the number of GPU clock cycles in a line and number of
/// lines in a frame (or field for interlaced output) depending on
/// the configured video mode
fn vmode_timings(&self) -> (u16, u16) {
// The number of ticks per line is an estimate using the
// average line length recorded by the timer1 using the
// "hsync" clock source.
match self.vmode {
VMode::Ntsc => (3412, 263),
VMode::Pal => (3404, 314),
}
}
/// Return the GPU to CPU clock ratio. The value is multiplied by
/// CLOCK_RATIO_FRAC to get a precise fixed point value.
fn gpu_to_cpu_clock_ratio(&self) -> FracCycles {
// First we convert the delta into GPU clock periods.
// GPU clock in Hz
let gpu_clock =
match self.standard {
VideoClock::Ntsc => 53_690_000.,
VideoClock::Pal => 53_222_000.,
};
// CPU clock in Hz
let cpu_clock = ::cpu::CPU_FREQ_HZ as f32;
// Clock ratio shifted 16bits to the left
FracCycles::from_f32(gpu_clock / cpu_clock)
}
/// Return the period of the dotclock expressed in CPU clock
/// periods
pub fn dotclock_period(&self) -> FracCycles {
let gpu_clock_period = self.gpu_to_cpu_clock_ratio();
let dotclock_divider = self.hres.dotclock_divider();
// Dividing the clock frequency means multiplying its period
let period = gpu_clock_period.get_fp() * dotclock_divider as Cycles;
FracCycles::from_fp(period)
}
/// Return the current phase of the GPU dotclock relative to the
/// CPU clock
pub fn dotclock_phase(&self) -> FracCycles {
panic!("GPU dotclock phase not implemented");
}
/// Return the period of the HSync signal in CPU clock periods
pub fn hsync_period(&self) -> FracCycles {
let (ticks_per_line, _) = self.vmode_timings();
let line_len = FracCycles::from_cycles(ticks_per_line as Cycles);
// Convert from GPU cycles into CPU cycles
line_len.divide(self.gpu_to_cpu_clock_ratio())
}
/// Return the phase of the hsync (position within the line) in
/// CPU clock periods.
pub fn hsync_phase(&self) -> FracCycles {
let phase = FracCycles::from_cycles(self.display_line_tick as Cycles);
let clock_phase = FracCycles::from_fp(self.gpu_clock_phase as Cycles);
let phase = phase.add(clock_phase);
// Convert phase from GPU clock cycles into CPU clock cycles
phase.multiply(self.gpu_to_cpu_clock_ratio())
}
/// Update the GPU state to its current status
pub fn sync(&mut self,
shared: &mut SharedState) {
let delta = shared.tk().sync(Peripheral::Gpu);
// Convert delta in GPU time, adding the leftover from the
// last time
let delta = self.gpu_clock_phase as Cycles +
delta * self.gpu_to_cpu_clock_ratio().get_fp();
// The 16 low bits are the new fractional part
self.gpu_clock_phase = delta as u16;
// Conwert delta back to integer
let delta = delta >> 16;
// Compute the current line and position within the line.
let (ticks_per_line, lines_per_frame) = self.vmode_timings();
let ticks_per_line = ticks_per_line as Cycles;
let lines_per_frame = lines_per_frame as Cycles;
let line_tick = self.display_line_tick as Cycles + delta;
let line = self.display_line as Cycles +
line_tick / ticks_per_line;
self.display_line_tick = (line_tick % ticks_per_line) as u16;
if line > lines_per_frame {
// New frame
if self.interlaced {
// Update the field
let nframes = line / lines_per_frame;
self.field =
match (nframes + self.field as Cycles) & 1 != 0 {
true => Field::Top,
false => Field::Bottom,
}
}
self.display_line = (line % lines_per_frame) as u16;
} else {
self.display_line = line as u16;
}
let vblank_interrupt = self.in_vblank();
if !self.vblank_interrupt && vblank_interrupt {
// Rising edge of the vblank interrupt
shared.irq_state_mut().assert(Interrupt::VBlank);
}
if self.vblank_interrupt && !vblank_interrupt {
// End of vertical blanking, we're starting a new frame
shared.counters_mut().frame.increment();
}
self.vblank_interrupt = vblank_interrupt;
self.predict_next_sync(shared);
}
/// Predict when the next "forced" sync should take place
pub fn predict_next_sync(&self, shared: &mut SharedState) {
let (ticks_per_line, lines_per_frame) = self.vmode_timings();
let ticks_per_line = ticks_per_line as Cycles;
let lines_per_frame = lines_per_frame as Cycles;
let mut delta = 0;
let cur_line = self.display_line as Cycles;
let display_line_start = self.display_line_start as Cycles;
let display_line_end = self.display_line_end as Cycles;
// Number of ticks to get to the start of the next line
delta += ticks_per_line - self.display_line_tick as Cycles;
// The various -1 in the next formulas are because we start
// counting at line 0. Without them we'd go one line too far.
if cur_line >= display_line_end {
// We're in the vertical blanking at the end of the
// frame. We want to synchronize at the end of the
// blanking at the beginning of the next frame.
// Number of ticks to get to the end of the frame
delta += (lines_per_frame - cur_line) * ticks_per_line;
// Numbef of ticks to get to the end of vblank in the next
// frame
delta += (display_line_start - 1) * ticks_per_line;
} else if cur_line < display_line_start {
// We're in the vertical blanking at the beginning of the
// frame. We want to synchronize at the end of the
// blanking for the current rame
delta += (display_line_start - 1 - cur_line) * ticks_per_line;
} else {
// We're in the active video, we want to synchronize at
// the beginning of the vertical blanking period
delta += (display_line_end - 1 - cur_line) * ticks_per_line;
}
// Convert delta in CPU clock periods.
delta <<= FracCycles::frac_bits();
// Remove the current fractional cycle to be more accurate
delta -= self.gpu_clock_phase as Cycles;
// Divide by the ratio while always rounding up to make sure
// we're never triggered too early
let ratio = self.gpu_to_cpu_clock_ratio().get_fp();
delta = (delta + ratio - 1) / ratio;
shared.tk().set_next_sync_delta(Peripheral::Gpu, delta);
}
pub fn display_vram_start(&self) -> (u16, u16) {
(self.display_vram_x_start, self.display_vram_y_start)
}
/// Return true if we're currently in the video blanking period
fn in_vblank(&self) -> bool {
self.display_line < self.display_line_start ||
self.display_line >= self.display_line_end
}
/// Return the index of the currently displayed VRAM line
fn displayed_vram_line(&self) -> u16 {
let offset =
match self.interlaced {
true => self.display_line * 2 + self.field as u16,
false => self.display_line,
};
// The VRAM "wraps around" so we in case of an overflow we
// simply truncate to 9bits
(self.display_vram_y_start + offset) & 0x1ff
}
pub fn load<T: Addressable>(&mut self,
shared: &mut SharedState,
offset: u32) -> u32 {
if T::size() != 4 {
panic!("Unhandled GPU load ({})", T::size());
}
self.sync(shared);
let r =
match offset {
0 => self.read(),
4 => self.status(),
_ => unreachable!(),
};
r
}
pub fn store<T: Addressable>(&mut self,
shared: &mut SharedState,
renderer: &mut Renderer,
timers: &mut Timers,
offset: u32,
val: u32) {
if T::size() != 4 {
panic!("Unhandled GPU load ({})", T::size());
}
self.sync(shared);
match offset {
0 => self.gp0(renderer, val),
4 => self.gp1(shared, renderer, val, timers),
_ => unreachable!(),
}
}
/// Dispatch to the current GP0 handler method
pub fn gp0(&mut self, renderer: &mut Renderer, val: u32) {
(self.gp0_handler)(self, renderer, val);
}
/// Retrieve value of the status register
fn status(&self) -> u32 {
let mut r = 0u32;
let draw_mode = self.draw_mode as u32;
r |= draw_mode & 0x7ff;
r |= ((draw_mode >> 11) & 1) << 15;
r |= (self.force_set_mask_bit as u32) << 11;
r |= (self.preserve_masked_pixels as u32) << 12;
r |= (self.field as u32) << 13;
// Bit 14: not supported
r |= self.hres.into_status();
r |= (self.vres as u32) << 19;
r |= (self.vmode as u32) << 20;
r |= (self.display_depth as u32) << 21;
r |= (self.interlaced as u32) << 22;
r |= (self.display_disabled as u32) << 23;
r |= (self.gp0_interrupt as u32) << 24;
// For now we pretend that the GPU is always ready:
// Ready to receive command
r |= 1 << 26;
// Ready to send VRAM to CPU
r |= 1 << 27;
// Ready to receive DMA block
r |= 1 << 28;
r |= (self.dma_direction as u32) << 29;
// Bit 31 is 1 if the currently displayed VRAM line is odd, 0
// if it's even or if we're in the vertical blanking.
if !self.in_vblank() {
r |= ((self.displayed_vram_line() & 1) as u32) << 31
}
// Not sure about that, I'm guessing that it's the signal
// checked by the DMA in when sending data in Request
// synchronization mode. For now I blindly follow the Nocash
// spec.
let dma_request =
match self.dma_direction {
// Always 0
DmaDirection::Off => 0,
// Should be 0 if FIFO is full, 1 otherwise
DmaDirection::Fifo => 1,
// Should be the same as status bit 28
DmaDirection::CpuToGp0 => (r >> 28) & 1,
// Should be the same as status bit 27
DmaDirection::VRamToCpu => (r >> 27) & 1,
};
r |= dma_request << 25;
r
}
/// Retrieve value of the "read" register
fn read(&self) -> u32 {
debug!("GPUREAD");
// XXX framebuffer read not supported
self.read_word
}
/// GP0 handler method: handle a command word
fn gp0_handle_command(&mut self, renderer: &mut Renderer, val: u32) {
let (len, attributes) = self.gp0_parse_command(val);
self.gp0_words_remaining = len;
self.gp0_attributes = attributes;
self.gp0_command.clear();
*self.gp0_handler = Gpu::gp0_handle_parameter;
// Call the parameter handling function for the current word
self.gp0_handle_parameter(renderer, val);
}
/// GP0 handler method: handle a command parameter
fn gp0_handle_parameter(&mut self, renderer: &mut Renderer, val: u32) {
self.gp0_command.push_word(val);
self.gp0_words_remaining -= 1;
if self.gp0_words_remaining == 0 {
// Command is complete and ready to run
//
// XXX certain commands (like quad drawing) actually start
// drawing the first triangle before the last vertex is
// received by the GPU
// Reset GP0 handler. Can be overriden by the callback in
// certain cases, for instance for image load commands.
*self.gp0_handler = Gpu::gp0_handle_command;
(self.gp0_attributes.callback)(self, renderer);
}
}
/// GP0 handler method: handle shaded polyline color word
fn gp0_handle_shaded_polyline_color(&mut self, _: &mut Renderer, val: u32) {
*self.gp0_handler =
if is_polyline_end_marker(val) {
// We found the end-of-polyline marker, we're done.
Gpu::gp0_handle_command
} else {
// Store the color and wait for the position in the
// next word
self.gp0_command.clear();
self.gp0_command.push_word(val);
Gpu::gp0_handle_shaded_polyline_vertex
};
}
/// GP0 handler method: handle shaded polyline vertex word
fn gp0_handle_shaded_polyline_vertex(&mut self,
renderer: &mut Renderer,
val: u32) {
// We don't test for the end-of-polyline marker here because
// it only works in color words for shaded polylines.
// The line starts at the end of the previous segment
let (start_pos, start_color) = self.polyline_prev;
// Retrieve color stored in `gp0_handle_shaded_polyline_color`
let end_color = gp0_color(self.gp0_command[0]);
let end_pos = gp0_position(val);
let vertices = [
Vertex::new(start_pos, start_color),
Vertex::new(end_pos, end_color),
];
renderer.push_line(self.gp0_attributes.primitive_attributes(),
&vertices);
// Store the new ending position for the next segment (if any)
self.polyline_prev = (end_pos, end_color);
// We expect the color of the next segment
*self.gp0_handler = Gpu::gp0_handle_shaded_polyline_color;
}
/// GP0 handler method: handle monochrome polyline position word
fn gp0_handle_monochrome_polyline_vertex(&mut self,
renderer: &mut Renderer,
val: u32) {
if is_polyline_end_marker(val) {
// We found the end-of-polyline marker, we're done.
*self.gp0_handler = Gpu::gp0_handle_command;
return;
}
// The line starts at the end of the previous segment
let (start_pos, color) = self.polyline_prev;
let end_pos = gp0_position(val);
let vertices = [
Vertex::new(start_pos, color),
Vertex::new(end_pos, color),
];
renderer.push_line(self.gp0_attributes.primitive_attributes(),
&vertices);
// Store the new ending position for the next segment (if any)
self.polyline_prev = (end_pos, color);
}
/// Parse GP0 command and return its length in words and attributes
fn gp0_parse_command(&self, gp0: u32) -> (u32, Gp0Attributes) {
let opcode = gp0 >> 24;
let dither = self.dither();
let (len, cback, dither): (u32, fn(&mut Gpu, &mut Renderer), bool) =
match opcode {
0x00 => (1, Gpu::gp0_nop, false),
0x01 => (1, Gpu::gp0_clear_cache, false),
0x02 => (3, Gpu::gp0_fill_rect, false),
0x20 => (4, Gpu::gp0_monochrome_triangle, false),
0x22 => (4, Gpu::gp0_monochrome_triangle, false),
0x24 => (7, Gpu::gp0_textured_triangle, dither),
0x25 => (7, Gpu::gp0_textured_triangle, dither),
0x26 => (7, Gpu::gp0_textured_triangle, dither),
0x27 => (7, Gpu::gp0_textured_triangle, dither),
0x28 => (5, Gpu::gp0_monochrome_quad, false),
0x2a => (5, Gpu::gp0_monochrome_quad, false),
0x2c => (9, Gpu::gp0_textured_quad, dither),
0x2d => (9, Gpu::gp0_textured_quad, dither),
0x2e => (9, Gpu::gp0_textured_quad, dither),
0x2f => (9, Gpu::gp0_textured_quad, dither),
0x30 => (6, Gpu::gp0_shaded_triangle, dither),
0x32 => (6, Gpu::gp0_shaded_triangle, dither),
0x34 => (9, Gpu::gp0_textured_shaded_triangle, dither),
0x36 => (9, Gpu::gp0_textured_shaded_triangle, dither),
0x38 => (8, Gpu::gp0_shaded_quad, dither),
0x3a => (8, Gpu::gp0_shaded_quad, dither),
0x3c => (12, Gpu::gp0_textured_shaded_quad, dither),
0x3e => (12, Gpu::gp0_textured_shaded_quad, dither),
0x40 => (3, Gpu::gp0_monochrome_line, false),
0x42 => (3, Gpu::gp0_monochrome_line, false),
0x48 => (3, Gpu::gp0_monochrome_polyline, false),
0x4a => (3, Gpu::gp0_monochrome_polyline, false),
0x50 => (4, Gpu::gp0_shaded_line, false),
0x52 => (4, Gpu::gp0_shaded_line, false),
0x58 => (4, Gpu::gp0_shaded_polyline, false),
0x5a => (4, Gpu::gp0_shaded_polyline, false),
0x60 => (3, Gpu::gp0_monochrome_rect, false),
0x62 => (3, Gpu::gp0_monochrome_rect, false),
0x64 => (4, Gpu::gp0_textured_rect, false),
0x65 => (4, Gpu::gp0_textured_rect, false),
0x66 => (4, Gpu::gp0_textured_rect, false),
0x67 => (4, Gpu::gp0_textured_rect, false),
0x68 => (2, Gpu::gp0_monochrome_rect_1x1, false),
0x6a => (2, Gpu::gp0_monochrome_rect_1x1, false),
0x74 => (3, Gpu::gp0_textured_rect_8x8, false),
0x75 => (3, Gpu::gp0_textured_rect_8x8, false),
0x76 => (3, Gpu::gp0_textured_rect_8x8, false),
0x77 => (3, Gpu::gp0_textured_rect_8x8, false),
0x78 => (2, Gpu::gp0_monochrome_rect_16x16, false),
0x7a => (2, Gpu::gp0_monochrome_rect_16x16, false),
0x7c => (3, Gpu::gp0_textured_rect_16x16, false),
0x7d => (3, Gpu::gp0_textured_rect_16x16, false),
0x7e => (3, Gpu::gp0_textured_rect_16x16, false),
0x7f => (3, Gpu::gp0_textured_rect_16x16, false),
0x80 => (4, Gpu::gp0_copy_rect, false),
0xa0 => (3, Gpu::gp0_image_load, false),
0xc0 => (3, Gpu::gp0_image_store, false),
0xe1 => (1, Gpu::gp0_draw_mode, false),
0xe2 => (1, Gpu::gp0_texture_window, false),
0xe3 => (1, Gpu::gp0_drawing_area_top_left, false),
0xe4 => (1, Gpu::gp0_drawing_area_bottom_right, false),
0xe5 => (1, Gpu::gp0_drawing_offset, false),
0xe6 => (1, Gpu::gp0_mask_bit_setting, false),
_ => panic!("Unhandled GP0 command {:08x}", gp0),
};
let textured = opcode & 0x4 != 0;
let blend_mode =
if textured {
if opcode & 1 != 0 {
BlendMode::Raw
} else {
BlendMode::Blended
}
} else {
BlendMode::None
};
let semi_transparent = opcode & 2 != 0;
let attr =
Gp0Attributes::new(cback,
semi_transparent,
blend_mode,
dither);
(len, attr)
}
fn dither(&self) -> bool {
(self.draw_mode >> 9) & 1 != 0
}
/// GP0(0x00): No operation
fn gp0_nop(&mut self, _: &mut Renderer) {
// NOP
}
/// GP0(0x01): Clear cache
fn gp0_clear_cache(&mut self, _: &mut Renderer) {
// XXX Not implemented
}
/// GP0(0x02): Fill rectangle
/// *Not* affected by mask setting unlike other rect commands
fn gp0_fill_rect(&mut self, renderer: &mut Renderer) {
let top_left = gp0_position(self.gp0_command[1]);
let size = gp0_position(self.gp0_command[2]);
let color = gp0_color(self.gp0_command[0]);
// Alignment constraints
let left = (top_left[0] & 0x3f0) as u16;
let top = (top_left[1] & 0x1ff) as u16;
// width = 0x400 (ignoring higher bits) is the same as width =
// 0, however width = 0x3ff is rounded up to 0x400 (without
// wrapping to 0) so we need to special case 0x400
let width =
match size[0] & 0x7ff {
0x400 => 0,
// round up to the next multiple of 0x10
n => ((n + 0xf) & 0x3f0) as u16,
};
let height = (size[1] & 0x1ff) as u16;
let mut bottom = top + height;
let mut right = left + width;
// XXX Normally the fill rect wraps around: if the x or y
// coordinates overflow we should wrap around the image.
if right > 0x400 {
warn!("Fill rect X overflow: {}", right);
right = 0x400;
}
if bottom > 0x200 {
warn!("Fill rect Y overflow: {}", bottom);
bottom = 0x200;
}
let width = right - left;
let height = bottom - top;
renderer.fill_rect(color,
(left, top),
(width, height));
}
/// Gp0(0x80): Copy rectangle
fn gp0_copy_rect(&mut self, _: &mut Renderer) {
let size = gp0_position(self.gp0_command[3]);
let src_top_left = gp0_position(self.gp0_command[1]);
let dst_top_left = gp0_position(self.gp0_command[2]);
// XXX Implement me
debug!("Copy Rectangle {:?} {:?} {:?}",
size, src_top_left, dst_top_left);
}
/// Draw an untextured unshaded triangle
fn gp0_monochrome_triangle(&mut self, renderer: &mut Renderer) {
let color = gp0_color(self.gp0_command[0]);
let vertices = [
Vertex::new(gp0_position(self.gp0_command[1]), color),
Vertex::new(gp0_position(self.gp0_command[2]), color),
Vertex::new(gp0_position(self.gp0_command[3]), color),
];
renderer.push_triangle(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw an untextured unshaded quad
fn gp0_monochrome_quad(&mut self, renderer: &mut Renderer) {
let color = gp0_color(self.gp0_command[0]);
let vertices = [
Vertex::new(gp0_position(self.gp0_command[1]), color),
Vertex::new(gp0_position(self.gp0_command[2]), color),
Vertex::new(gp0_position(self.gp0_command[3]), color),
Vertex::new(gp0_position(self.gp0_command[4]), color),
];
renderer.push_quad(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw a monochrome line
fn gp0_monochrome_line(&mut self, renderer: &mut Renderer) {
let color = gp0_color(self.gp0_command[0]);
let vertices = [
Vertex::new(gp0_position(self.gp0_command[1]), color),
Vertex::new(gp0_position(self.gp0_command[2]), color),
];
renderer.push_line(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw a monochrome polyline
fn gp0_monochrome_polyline(&mut self, renderer: &mut Renderer) {
// Start with the first segment. The end-of-polyline marker is
// ignored for the first two vertices.
let color = gp0_color(self.gp0_command[0]);
let start_pos = gp0_position(self.gp0_command[1]);
let end_pos = gp0_position(self.gp0_command[2]);
let vertices = [
Vertex::new(start_pos, color),
Vertex::new(end_pos, color),
];
renderer.push_line(self.gp0_attributes.primitive_attributes(),
&vertices);
// Store the end point to continue the polyline when we get
// the next vertex
self.polyline_prev = (end_pos, color);
*self.gp0_handler = Gpu::gp0_handle_monochrome_polyline_vertex;
}
/// Draw a textured unshaded triangle
fn gp0_textured_triangle(&mut self, renderer: &mut Renderer) {
let color = gp0_color(self.gp0_command[0]);
self.gp0_attributes.set_clut(self.gp0_command[2] >> 16);
self.gp0_attributes.set_draw_params(self.gp0_command[4] >> 16);
let vertices = [
Vertex::new_textured(gp0_position(self.gp0_command[1]),
color,
gp0_texture_coordinates(self.gp0_command[2])),
Vertex::new_textured(gp0_position(self.gp0_command[3]),
color,
gp0_texture_coordinates(self.gp0_command[4])),
Vertex::new_textured(gp0_position(self.gp0_command[5]),
color,
gp0_texture_coordinates(self.gp0_command[6])),
];
renderer.push_triangle(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw a textured unshaded quad
fn gp0_textured_quad(&mut self, renderer: &mut Renderer) {
let color = gp0_color(self.gp0_command[0]);
self.gp0_attributes.set_clut(self.gp0_command[2] >> 16);
self.gp0_attributes.set_draw_params(self.gp0_command[4] >> 16);
let vertices = [
Vertex::new_textured(gp0_position(self.gp0_command[1]),
color,
gp0_texture_coordinates(self.gp0_command[2])),
Vertex::new_textured(gp0_position(self.gp0_command[3]),
color,
gp0_texture_coordinates(self.gp0_command[4])),
Vertex::new_textured(gp0_position(self.gp0_command[5]),
color,
gp0_texture_coordinates(self.gp0_command[6])),
Vertex::new_textured(gp0_position(self.gp0_command[7]),
color,
gp0_texture_coordinates(self.gp0_command[8])),
];
renderer.push_quad(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw an untextured shaded triangle
fn gp0_shaded_triangle(&mut self, renderer: &mut Renderer) {
let vertices = [
Vertex::new(gp0_position(self.gp0_command[1]),
gp0_color(self.gp0_command[0])),
Vertex::new(gp0_position(self.gp0_command[3]),
gp0_color(self.gp0_command[2])),
Vertex::new(gp0_position(self.gp0_command[5]),
gp0_color(self.gp0_command[4])),
];
renderer.push_triangle(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw an untextured shaded quad
fn gp0_shaded_quad(&mut self, renderer: &mut Renderer) {
let vertices = [
Vertex::new(gp0_position(self.gp0_command[1]),
gp0_color(self.gp0_command[0])),
Vertex::new(gp0_position(self.gp0_command[3]),
gp0_color(self.gp0_command[2])),
Vertex::new(gp0_position(self.gp0_command[5]),
gp0_color(self.gp0_command[4])),
Vertex::new(gp0_position(self.gp0_command[7]),
gp0_color(self.gp0_command[6])),
];
renderer.push_quad(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw a shaded line
fn gp0_shaded_line(&mut self, renderer: &mut Renderer) {
let vertices = [
Vertex::new(gp0_position(self.gp0_command[1]),
gp0_color(self.gp0_command[0])),
Vertex::new(gp0_position(self.gp0_command[3]),
gp0_color(self.gp0_command[2])),
];
renderer.push_line(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw a shaded polyline
fn gp0_shaded_polyline(&mut self, renderer: &mut Renderer) {
// Start with the first segment. We cannot have an
// end-of-polyline marker in any of these vertice's color code
// (if you put the marker in the 2nd vertex color word it's
// ignored, it only works starting from the third vertex
// onwards)
let start_color = gp0_color(self.gp0_command[0]);
let start_pos = gp0_position(self.gp0_command[1]);
let end_color = gp0_color(self.gp0_command[2]);
let end_pos = gp0_position(self.gp0_command[3]);
let vertices = [
Vertex::new(start_pos, start_color),
Vertex::new(end_pos, end_color),
];
renderer.push_line(self.gp0_attributes.primitive_attributes(),
&vertices);
// Store the end point to continue the polyline when we get
// the next vertex
self.polyline_prev = (end_pos, end_color);
*self.gp0_handler = Gpu::gp0_handle_shaded_polyline_color;
}
/// Draw a textured shaded triangle
fn gp0_textured_shaded_triangle(&mut self, renderer: &mut Renderer) {
self.gp0_attributes.set_clut(self.gp0_command[2] >> 16);
self.gp0_attributes.set_draw_params(self.gp0_command[5] >> 16);
let vertices = [
Vertex::new_textured(gp0_position(self.gp0_command[1]),
gp0_color(self.gp0_command[0]),
gp0_texture_coordinates(self.gp0_command[2])),
Vertex::new_textured(gp0_position(self.gp0_command[4]),
gp0_color(self.gp0_command[3]),
gp0_texture_coordinates(self.gp0_command[5])),
Vertex::new_textured(gp0_position(self.gp0_command[7]),
gp0_color(self.gp0_command[6]),
gp0_texture_coordinates(self.gp0_command[8])),
];
renderer.push_triangle(self.gp0_attributes.primitive_attributes(),
&vertices);
}
/// Draw a textured shaded quad
fn gp0_textured_shaded_quad(&mut self, renderer: &mut Renderer) {
self.gp0_attributes.set_clut(self.gp0_command[2] >> 16);
self.gp0_attributes.set_draw_params(self.gp0_command[5] >> 16);
let vertices = [
Vertex::new_textured(gp0_position(self.gp0_command[1]),
gp0_color(self.gp0_command[0]),
gp0_texture_coordinates(self.gp0_command[2])),
Vertex::new_textured(gp0_position(self.gp0_command[4]),
gp0_color(self.gp0_command[3]),
gp0_texture_coordinates(self.gp0_command[5])),
Vertex::new_textured(gp0_position(self.gp0_command[7]),
gp0_color(self.gp0_command[6]),
gp0_texture_coordinates(self.gp0_command[8])),
Vertex::new_textured(gp0_position(self.gp0_command[10]),
gp0_color(self.gp0_command[9]),
gp0_texture_coordinates(self.gp0_command[11])),
];
renderer.push_quad(self.gp0_attributes.primitive_attributes(),
&vertices);
}
fn gp0_rect_sized(&mut self,
renderer: &mut Renderer,