Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
uv-overflow.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// Overflow probes. Each test draws one oversized 8-bit textured rect
28// based at the test origin (0,0) with a chosen base UV, then reads
29// back a column (U sweep, row v=0) or row (V sweep, col u=0). The
30// fixture is texel(u,v) = (u+v)&0xff, so the texel sampled at
31// primitive-offset k from base b is ((b + k) & 0xff) iff the texel
32// coordinate wraps mod 256. Each expectation is therefore
33// expectedClut8Color((base + offset) & 0xff).
34
35// clang-format off
36
38
39// Draw an 8-bit textured rect at the origin with the given base UV and
40// size. Clears the rect footprint to the sentinel first so any pixel
41// that did NOT draw (e.g. a coordinate sampling transparent CLUT[0])
42// is distinguishable from a wrapped fetch.
43static void drawTex8Rect(uint8_t u0, uint8_t v0, int16_t w, int16_t h) {
44 rasterReset();
45 rasterClearTestRegion(0, 0, (int16_t)((w + 1) & ~1), h);
46 setTexpage(TEX8_TX, TEX8_TY, 1);
47 setTextureWindow(0, 0, 0, 0);
48 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, u0, v0, w, h, CLUT8_FIELD);
49 rasterFlushPrimitive();
50}
51
52) // CESTER_BODY
53
54// ============================================================================
55// U overflow. Wide rect (264 px) based at u=0. Columns 0..255 map to
56// u=0..255 (in range); columns 256..263 must wrap to u=0..7.
57// ============================================================================
58
59CESTER_TEST(ovf_u_base0_c5, gpu_raster_phase21,
60 drawTex8Rect(0, 0, 264, 2);
61 /* Control: in-range column, no overflow. u=5. */
62 ASSERT_PIXEL_EQ(expectedClut8Color(5), 5, 0);
63)
64
65CESTER_TEST(ovf_u_base0_c200, gpu_raster_phase21,
66 drawTex8Rect(0, 0, 264, 2);
67 /* Control: deep in-range column. u=200. */
68 ASSERT_PIXEL_EQ(expectedClut8Color(200), 200, 0);
69)
70
71CESTER_TEST(ovf_u_base0_c255, gpu_raster_phase21,
72 drawTex8Rect(0, 0, 264, 2);
73 /* Last in-range column. u=255. */
74 ASSERT_PIXEL_EQ(expectedClut8Color(255), 255, 0);
75)
76
77CESTER_TEST(ovf_u_base0_c256, gpu_raster_phase21,
78 drawTex8Rect(0, 0, 264, 2);
79 /* First overflow column: u counter hits 256 -> wraps to 0. */
80 ASSERT_PIXEL_EQ(expectedClut8Color(256 & 0xff), 256, 0);
81)
82
83CESTER_TEST(ovf_u_base0_c257, gpu_raster_phase21,
84 drawTex8Rect(0, 0, 264, 2);
85 /* Overflow -> u=1. */
86 ASSERT_PIXEL_EQ(expectedClut8Color(257 & 0xff), 257, 0);
87)
88
89CESTER_TEST(ovf_u_base0_c263, gpu_raster_phase21,
90 drawTex8Rect(0, 0, 264, 2);
91 /* Overflow -> u=7. */
92 ASSERT_PIXEL_EQ(expectedClut8Color(263 & 0xff), 263, 0);
93)
94
95// ============================================================================
96// U overflow with a non-zero base. Base u=200, width 64 -> the wrap
97// happens mid-rect at column 56 (200+56 = 256 -> 0).
98// ============================================================================
99
100CESTER_TEST(ovf_u_base200_c55, gpu_raster_phase21,
101 drawTex8Rect(200, 0, 64, 2);
102 /* u = 200+55 = 255, last before wrap. */
103 ASSERT_PIXEL_EQ(expectedClut8Color((200 + 55) & 0xff), 55, 0);
104)
105
106CESTER_TEST(ovf_u_base200_c56, gpu_raster_phase21,
107 drawTex8Rect(200, 0, 64, 2);
108 /* u = 200+56 = 256 -> wraps to 0. */
109 ASSERT_PIXEL_EQ(expectedClut8Color((200 + 56) & 0xff), 56, 0);
110)
111
112CESTER_TEST(ovf_u_base200_c57, gpu_raster_phase21,
113 drawTex8Rect(200, 0, 64, 2);
114 /* u -> 1. */
115 ASSERT_PIXEL_EQ(expectedClut8Color((200 + 57) & 0xff), 57, 0);
116)
117
118// ============================================================================
119// V overflow. Tall rect (264 px) based at v=0, sampled at col u=0.
120// Rows 0..255 map to v=0..255 (in range); rows 256..263 must wrap.
121// ============================================================================
122
123CESTER_TEST(ovf_v_base0_r5, gpu_raster_phase21,
124 drawTex8Rect(0, 0, 2, 264);
125 /* Control: in-range row. v=5 -> texel(0,5)=5. */
126 ASSERT_PIXEL_EQ(expectedClut8Color(5), 0, 5);
127)
128
129CESTER_TEST(ovf_v_base0_r255, gpu_raster_phase21,
130 drawTex8Rect(0, 0, 2, 264);
131 /* Last in-range row. v=255. */
132 ASSERT_PIXEL_EQ(expectedClut8Color(255), 0, 255);
133)
134
135CESTER_TEST(ovf_v_base0_r256, gpu_raster_phase21,
136 drawTex8Rect(0, 0, 2, 264);
137 /* First overflow row: v counter hits 256 -> wraps to 0. */
138 ASSERT_PIXEL_EQ(expectedClut8Color(256 & 0xff), 0, 256);
139)
140
141CESTER_TEST(ovf_v_base0_r257, gpu_raster_phase21,
142 drawTex8Rect(0, 0, 2, 264);
143 /* Overflow -> v=1. */
144 ASSERT_PIXEL_EQ(expectedClut8Color(257 & 0xff), 0, 257);
145)
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
volatile void * u0
Definition load-timings.c:875
#define ASSERT_PIXEL_EQ(expected, x_, y_)
Definition raster-helpers.h:473
#define CLUT8_FIELD
Definition texture-fixtures.h:84
#define TEX8_TY
Definition texture-fixtures.h:61
#define TEX8_TX
Definition texture-fixtures.h:60
#define TEX_MOD_NEUTRAL
Definition texture-fixtures.h:308