Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
mask-bit.c
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2026 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27// Mask-bit suite. GP0(E6) sets two bits of GPU state:
28// bit 0: set-mask - drawn pixels have VRAM bit 15 forced to 1
29// bit 1: check-mask - skip writes to VRAM pixels with bit 15 == 1
30//
31// Tests:
32// 1. set-mask only: drawn pixels should be 0x801f (RED + mask bit).
33// 2. set-mask then check-mask overlay: mask-set pixels preserved,
34// non-mask pixels overwritten.
35//
36// SENTINEL OVERRIDE: the default RASTER_SENTINEL (0xDEAD) has bit 15
37// set. Check-mask would skip writes to pixels containing the sentinel,
38// which conflates "rasterizer chose to skip" with "sentinel collided".
39// Per the documented mitigation in raster-helpers.h, this suite uses a
40// local mask-clear sentinel.
41
42#define MASK_SUITE_SENTINEL 0x5555u /* R=21,G=10,B=21,mask=0 - no bit 15 */
43
44// clang-format off
45
47
48// Set-mask + draw RED triangle (same as triangle A but with E6=0x01).
49static void rasterDrawMaskSet(void) {
50 rasterReset();
51 // Use the local mask-clear sentinel so absence-tests work even
52 // though check-mask is not active in this draw - keeps the suite
53 // internally consistent.
54 rasterFillRect(0, 0, 16, 16, MASK_SUITE_SENTINEL);
55 // GP0(E6): set bit 0 (set-mask). Pixels drawn after this command
56 // get VRAM bit 15 forced to 1.
57 sendGPUData(0xe6000001u);
58 rasterFlatTri(RASTER_CMD_RED, 0, 0, 4, 0, 0, 4);
59 rasterFlushPrimitive();
60}
61
62// Set-mask first triangle, then check-mask second overlapping triangle.
63// Geometry: tri A (0,0)(4,0)(0,4) RED with set-mask, then tri (4,0)(8,0)(4,4)
64// GREEN with check-mask. They overlap on the shared edge from (4,0)
65// down; mask-set RED pixels should preserve, non-overlap GREEN pixels
66// should fill.
67static void rasterDrawMaskCheck(void) {
68 rasterReset();
69 rasterFillRect(0, 0, 16, 16, MASK_SUITE_SENTINEL);
70
71 // First pass: RED tri with set-mask, no check.
72 sendGPUData(0xe6000001u);
73 rasterFlatTri(RASTER_CMD_RED, 0, 0, 4, 0, 0, 4);
74 rasterFlushPrimitive();
75
76 // Second pass: GREEN tri with check-mask, no set.
77 sendGPUData(0xe6000002u);
78 rasterFlatTri(RASTER_CMD_GREEN, 4, 0, 8, 0, 4, 4);
79 rasterFlushPrimitive();
80
81 // Restore mask state to default for subsequent tests.
82 sendGPUData(0xe6000000u);
83}
84
85) // CESTER_BODY
86
87// --------------------------------------------------------------------------
88// Set-mask: drawn pixels have bit 15 forced
89// --------------------------------------------------------------------------
90
91CESTER_TEST(maskSet_origin_has_mask_bit, gpu_raster_phase2,
92 rasterDrawMaskSet();
94)
95
96CESTER_TEST(maskSet_interior_has_mask_bit, gpu_raster_phase2,
97 rasterDrawMaskSet();
99)
100
101CESTER_TEST(maskSet_right_edge_excluded, gpu_raster_phase2,
102 rasterDrawMaskSet();
103 // Note: this suite uses MASK_SUITE_SENTINEL (0x5555) not RASTER_SENTINEL.
104 // The macro in raster-expected-phase2.h still references the global
105 // RASTER_SENTINEL for absence cases; we override locally here.
106 ASSERT_PIXEL_EQ((unsigned)MASK_SUITE_SENTINEL, 4, 0);
107)
108
109// --------------------------------------------------------------------------
110// Check-mask: pixels already masked are preserved
111// --------------------------------------------------------------------------
112
113CESTER_TEST(maskCheck_preserves_red_at_overlap_1_0, gpu_raster_phase2,
114 rasterDrawMaskCheck();
115 // (1, 0) is in both triangles. First pass (RED + set-mask) writes
116 // 0x801f. Second pass (GREEN + check-mask) should see bit 15 set
117 // and skip, preserving 0x801f.
119)
120
121CESTER_TEST(maskCheck_fills_green_in_non_overlap_5_0, gpu_raster_phase2,
122 rasterDrawMaskCheck();
123 // (5, 0) is in GREEN tri only and the local sentinel 0x5555 has bit
124 // 15 clear, so check-mask should allow the write. Expect GREEN.
126)
CESTER_TEST(cddaLevelHalf, test_instances, static const uint8_t atv[4]={0x40, 0x00, 0x40, 0x00};unsigned changes=0;int peak=cddaPeak(atv, 1, &changes);ramsyscall_printf("CD-DA level at half ATV: peak %i, %u sign changes\n", peak, changes);cester_assert_int_ge(peak, 11300);cester_assert_int_le(peak, 11800);cester_assert_uint_ge(changes, 4);) CESTER_TEST(cddaLevelClipped
CESTER_BODY(static uint16_t s_cddaCapture;static int cddaSpuDma(uint32_t spuAddr, void *buf, uint32_t bytes, int isRead) { const uint16_t tsa=spuAddr > > 3;const uint16_t mode=isRead ? 0x30 :0x20;SPU_RAM_DTA=tsa;for(int i=0;i< 0xf01 &&SPU_RAM_DTA !=tsa;i++);SPU_CTRL=(SPU_CTRL &~0x30)|mode;for(volatile int i=0;i< 60;i++);for(int i=0;i< 0xf01 &&(SPU_STATUS &0x30) !=mode;i++);CDDA_SPU_DELAY=(CDDA_SPU_DELAY &0xf0ffffff)|(isRead ? 0x22000000 :0x20000000);DMA_CTRL[DMA_SPU].MADR=(uint32_t) buf &0x1fffffff;DMA_CTRL[DMA_SPU].BCR=((bytes > > 6)<< 16)|0x10;DMA_CTRL[DMA_SPU].CHCR=isRead ? 0x01000200 :0x01000201;int ok=0;for(unsigned t=0;t< 4000000u;t++) { if((DMA_CTRL[DMA_SPU].CHCR &0x01000000)==0) { ok=1;break;} } SPU_CTRL=SPU_CTRL &~0x30;for(volatile int i=0;i< 60;i++);return ok;} static int cddaSimpleCmd(uint8_t cmd) { uint8_t response[16];CDROM_REG0=0;CDROM_REG1=cmd;uint32_t timeout=2000000;int ok=waitCDRomIRQWithTimeout(&timeout);ackCDRomCause();readResponse(response);return ok;} static void cddaWaitPauseComplete() { initializeTime();uint32_t elapsed=0;while(elapsed< 5000000u) { uint32_t budget=200000u;int ok=waitCDRomIRQWithTimeout(&budget);elapsed=budget;if(!ok) continue;uint8_t cause=ackCDRomCause();uint8_t response[16];readResponse(response);if(cause==2) return;} } static int cddaAudioSetup(const uint8_t atv[4], int demute) { if(!resetCDRom()) return 0;DPCR|=0x000b0000;SPU_CTRL=0;for(volatile int i=0;i< 20000;i++);SPU_VOL_MAIN_LEFT=0;SPU_VOL_MAIN_RIGHT=0;SPU_KEY_OFF_LOW=0xffff;SPU_KEY_OFF_HIGH=0xffff;SPU_RAM_DTC=4;SPU_CTRL=0x8001;for(volatile int i=0;i< 20000;i++);for(int i=0;i< 0x400;i++) s_cddaCapture[i]=CDDA_MARKER;if(!cddaSpuDma(0x000, s_cddaCapture, 0x800, 0)) return 0;if(!cddaSimpleCmd(demute ? CDL_DEMUTE :CDL_MUTE)) return 0;CDROM_REG0=2;CDROM_REG2=atv[0];CDROM_REG3=atv[1];CDROM_REG0=3;CDROM_REG1=atv[2];CDROM_REG2=atv[3];CDROM_REG0=3;CDROM_REG3=0x20;CDROM_REG0=0;SPU_VOL_CD_LEFT=0x7fff;SPU_VOL_CD_RIGHT=0x7fff;SPU_CTRL=SPU_CTRL|0x8001;return 1;} static int cddaBufferPeak(unsigned *signChanges) { int peak=0;unsigned changes=0;int prev=0;for(int i=0;i< 0x200;i++) { int v=(int16_t) s_cddaCapture[i];int a=v< 0 ? -v :v;if(a > peak) peak=a;int sign=v >=0 ? 1 :-1;if(i &&sign !=prev) changes++;prev=sign;} *signChanges=changes;return peak;} static int cddaCapturePeak(unsigned *signChanges) { int peak=0;for(unsigned attempt=0;attempt< 8;attempt++) { if(attempt) { initializeTime();while(updateTime()< 250000u);} if(!cddaSpuDma(0x000, s_cddaCapture, 0x800, 1)) return -1;peak=cddaBufferPeak(signChanges);if(peak) { if(attempt) ramsyscall_printf("capture was empty %u time(s) before audio\n", attempt);break;} } return peak;} static int cddaPeak(const uint8_t atv[4], int demute, unsigned *signChanges) { if(!cddaAudioSetup(atv, demute)) return -1;if(!setMode(0)) return -1;CDROM_REG0=0;CDROM_REG2=0x02;if(!cddaSimpleCmd(CDL_PLAY)) return -1;initializeTime();while(updateTime()< 1500000u);int peak=cddaCapturePeak(signChanges);CDROM_REG0=0;CDROM_REG1=CDL_PAUSE;cddaWaitPauseComplete();CDROM_REG0=0;CDROM_REG2=0x00;CDROM_REG2=0x02;CDROM_REG2=0x16;if(cddaSimpleCmd(CDL_SETLOC) &&cddaSimpleCmd(CDL_SEEKL)) cddaWaitPauseComplete();return peak;}[0x400])
Definition cdda.c:37
sendGPUData(0xe1000000)
#define MASK_SUITE_SENTINEL
Definition mask-bit.c:42
#define EXPECT_MASK_CHECK_PIXEL_5_0_filled
Definition raster-expected-phase2.h:115
#define EXPECT_MASK_SET_PIXEL_0_0
Definition raster-expected-phase2.h:101
#define EXPECT_MASK_SET_PIXEL_2_1
Definition raster-expected-phase2.h:102
#define EXPECT_MASK_CHECK_PIXEL_1_0_preserved
Definition raster-expected-phase2.h:114
#define RASTER_CMD_RED
Definition raster-helpers.h:124
#define RASTER_CMD_GREEN
Definition raster-helpers.h:126
#define ASSERT_PIXEL_EQ(expected, x_, y_)
Definition raster-helpers.h:473