ovl-upload/ovl-upload.c

560 lines
16 KiB
C
Raw Normal View History

2021-04-29 18:46:10 +02:00
/* ovl-upload.c, by ABelliqueux, 04-2021 license GNU General Public License v3.0
This example code demonstrates how to use the companion 'ovl-upload.py' script that should be provided with this file.
Once the code is loaded on a unirom enabled PSX via a serial/USB cable, 'ovl-upload.py' listens for a specific command
to load an overlay file on demand.
For an explanation about overlays, see http://psx.arthus.net/sdk/Psy-Q/DOCS/TRAINING/FALL96/overlay.pdf
For a basic example see @JaberwockySeamonstah's https://github.com/JaberwockySeamonstah/PSXOverlayExample
Unirom can be found here : https://github.com/JonathanDotCel/unirom8_bootdisc_and_firmware_for_ps1
with it's companion pc side software : https://github.com/JonathanDotCel/NOTPSXSerial
Thanks to @JaberwockySeamonstah, @JonathanDotCel, @nicolasnoble, @Lameguy64 for their help and patience.
2021-04-29 19:05:54 +02:00
Demonstrates:
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
* Using overlays to store different data and loading them in memory as needed.
Controls:
Select - Load alternative overlay
2021-04-29 18:46:10 +02:00
*/
#include <sys/types.h>
#include <libgte.h>
#include <libgpu.h>
#include <libetc.h>
#include <stdio.h>
2021-05-04 17:26:44 +02:00
#include <libsio.h>
#include <string.h>
2021-04-29 18:46:10 +02:00
2021-04-30 16:42:24 +02:00
#include "pcdrv.h"
2021-04-29 18:46:10 +02:00
// If USECD is defined, files will be loaded from the CD. Use this method for testing in an emulator.
// Additionaly, generate the bin/cue with mkpsxiso.
//~ #define USECD
#ifdef USECD
#include <libcd.h>
#endif
// Sample vector models
#include "tritex.h"
#include "cubetex.h"
#define VMODE 0
#define SCREENXRES 320
#define SCREENYRES 240
2021-04-29 19:05:54 +02:00
#define CENTERX SCREENXRES/2
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
#define CENTERY SCREENYRES/2
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
#define OTLEN 2048 // Maximum number of OT entries
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
#define PRIMBUFFLEN 32768 // Maximum number of POLY_GT3 primitives
2021-04-29 18:46:10 +02:00
// Display and draw environments, double buffered
DISPENV disp[2];
DRAWENV draw[2];
2021-04-29 19:05:54 +02:00
u_long ot[2][OTLEN]; // Ordering table (contains addresses to primitives)
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
char primbuff[2][PRIMBUFFLEN] = {0}; // Primitive list // That's our prim buffer
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
//~ int primcnt=0; // Primitive counter
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
char * nextpri = primbuff[0]; // Primitive counter
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
short db = 0; // Current buffer counter
2021-04-29 18:46:10 +02:00
// Texture image
extern unsigned long _binary_TIM_cubetex_tim_start[];
extern unsigned long _binary_TIM_cubetex_tim_end[];
extern unsigned long _binary_TIM_cubetex_tim_length;
TIM_IMAGE tim_cube;
// OVERLAYS CONFIG
// These symbols name are defined in 'overlay.ld', l.8, l.24 and l.41
// Use &load_all_overlays_here to get the memory adress where the overlay files are loaded.
// Those adresses you can check in the generated .map file at compile time.
extern u_long load_all_overlays_here;
extern u_long __lvl0_end; // Use &__lvl0_end to get end address of corresponding overlay.
extern u_long __lvl1_end;
//~ u_long overlaySize = 0;
2021-04-30 16:42:24 +02:00
char * overlayFile; // Will hold the name of the file to load.
char * ptrToChar[8]; // Will hold the name of the file to load.
volatile u_char loadFileID = 0; // Will hold an ID that's unique for each file.
2021-04-29 18:46:10 +02:00
2021-05-01 14:51:25 +02:00
u_char overlayFileID = 0, loadFileIDwas = 0;
2021-04-29 18:46:10 +02:00
// Timer for the pad
u_short timer = 0;
2021-04-30 16:42:24 +02:00
// pcdrv protocol
2021-05-04 17:26:44 +02:00
u_char escape = 0x00; // Hypothetical Escape char for unirom
2021-04-30 16:42:24 +02:00
2021-05-04 17:26:44 +02:00
u_char protocol = 0x01; // Hypothetical ID number for the pcdrv protocol in unirom
2021-04-30 16:42:24 +02:00
u_char command = LOAD; // We're loading the data here
2021-05-01 14:51:25 +02:00
uint32_t checkSum = 0;
2021-05-04 17:26:44 +02:00
const char * inBuffer = "";
char byte;
2021-04-29 18:46:10 +02:00
// Prototypes
void init(void);
void display(void);
void LoadTexture(u_long * tim, TIM_IMAGE * tparam);
void init(){
// Reset the GPU before doing anything and the controller
2021-04-29 19:05:54 +02:00
PadInit(0);
ResetGraph(0);
// Initialize and setup the GTE
InitGeom();
SetGeomOffset(CENTERX, CENTERY); // x, y offset
SetGeomScreen(CENTERX); // Distance between eye and screen
// Set the display and draw environments
SetDefDispEnv(&disp[0], 0, 0 , SCREENXRES, SCREENYRES);
SetDefDispEnv(&disp[1], 0, SCREENYRES, SCREENXRES, SCREENYRES);
SetDefDrawEnv(&draw[0], 0, SCREENYRES, SCREENXRES, SCREENYRES);
SetDefDrawEnv(&draw[1], 0, 0, SCREENXRES, SCREENYRES);
2021-04-29 18:46:10 +02:00
if (VMODE)
{
SetVideoMode(MODE_PAL);
disp[0].screen.y += 8;
disp[1].screen.y += 8;
}
2021-04-29 19:05:54 +02:00
2021-04-29 18:46:10 +02:00
setRGB0(&draw[0], 0, 0, 255);
setRGB0(&draw[1], 0, 0, 255);
draw[0].isbg = 1;
draw[1].isbg = 1;
PutDispEnv(&disp[db]);
2021-04-29 19:05:54 +02:00
PutDrawEnv(&draw[db]);
// Init font system
FntLoad(960, 0);
FntOpen(16, 16, 196, 64, 0, 256);
2021-04-29 18:46:10 +02:00
}
void display(void){
DrawSync(0);
VSync(0);
PutDispEnv(&disp[db]);
PutDrawEnv(&draw[db]);
SetDispMask(1);
DrawOTag(ot[db] + OTLEN - 1);
db = !db;
nextpri = primbuff[db];
}
void LoadTexture(u_long * tim, TIM_IMAGE * tparam){ // This part is from Lameguy64's tutorial series : lameguy64.net/svn/pstutorials/chapter1/3-textures.html login/pw: annoyingmous
2021-04-29 19:05:54 +02:00
OpenTIM(tim); // Open the tim binary data, feed it the address of the data in memory
ReadTIM(tparam); // This read the header of the TIM data and sets the corresponding members of the TIM_IMAGE structure
2021-04-29 18:46:10 +02:00
LoadImage(tparam->prect, tparam->paddr); // Transfer the data from memory to VRAM at position prect.x, prect.y
2021-04-29 19:05:54 +02:00
DrawSync(0); // Wait for the drawing to end
if (tparam->mode & 0x8){ // check 4th bit // If 4th bit == 1, TIM has a CLUT
LoadImage(tparam->crect, tparam->caddr); // Load it to VRAM at position crect.x, crect.y
DrawSync(0); // Wait for drawing to end
}
2021-04-29 18:46:10 +02:00
}
2021-05-04 17:26:44 +02:00
//~ static inline uint32_t djbProcess(uint32_t hash, const char str[], unsigned n) {
//~ return n ? djbProcess ( ( ( hash << 5 ) + hash ) ^ str[0], str + 1, n - 1) : hash;
//~ }
//~ static inline uint32_t djbHash( const char* str, unsigned n ){
//~ return djbProcess( 5381, str, n);
//~ }
2021-04-29 18:46:10 +02:00
int main() {
// Update this value to avoid trigger at launch
loadFileIDwas = overlayFileID = loadFileID;
if ( loadFileID == 0 ){
overlayFile = "\\cube.bin;1";
} else if ( loadFileID == 1) {
overlayFile = "\\tri.bin;1";
}
// Load overlay from CD if definde
#ifdef USECD
CdInit();
int cdread = 0, cdsync = 1;
2021-04-29 19:05:54 +02:00
2021-04-29 18:46:10 +02:00
cdread = CdReadFile( (char *)(overlayFile), &load_all_overlays_here, 0);
2021-04-29 19:05:54 +02:00
2021-04-29 18:46:10 +02:00
cdsync = CdReadSync(0, 0);
#endif
2021-04-29 19:05:54 +02:00
int i;
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
int PadStatus;
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
int TPressed=0;
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
int AutoRotate=1;
long t, p, OTz, Flag; // t == vertex count, p == depth cueing interpolation value, OTz == value to create Z-ordered OT, Flag == see LibOver47.pdf, p.143
2021-04-29 18:46:10 +02:00
MESH * model = &Tri;
POLY_GT3 *poly = {0}; // pointer to a POLY_GT3
2021-04-29 19:05:54 +02:00
SVECTOR Rotate={ 0 }; // Rotation coordinates
VECTOR Trans={ 0, 0, CENTERX, 0 }; // Translation coordinates
VECTOR Scale={ ONE, ONE, ONE, 0 }; // ONE == 4096
MATRIX Matrix={0}; // Matrix data for the GTE
2021-04-29 18:46:10 +02:00
// Texture window
DR_MODE * dr_mode; // Pointer to dr_mode prim
2021-04-29 19:05:54 +02:00
RECT tws = {0, 0, 32, 32}; // Texture window coordinates : x, y, w, h
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
init();
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
LoadTexture(_binary_TIM_cubetex_tim_start, &tim_cube);
2021-04-29 18:46:10 +02:00
2021-05-04 17:26:44 +02:00
//~ static u_char SIOinit = 0;
//~ static u_char SIO = 0;
2021-04-29 19:05:54 +02:00
// Main loop
while (1) {
2021-04-29 18:46:10 +02:00
// Overlay switch
if ( loadFileID != loadFileIDwas ){
// Update previous file value
loadFileIDwas = loadFileID;
// Change file to load
switch ( loadFileID ){
case 0:
overlayFile = "\\cube.bin;1";
overlayFileID = 0;
break;
case 1:
overlayFile = "\\tri.bin;1";
overlayFileID = 1;
break;
default:
overlayFile = "\\cube.bin;1";
overlayFileID = 0;
break;
}
#ifdef USECD
cdread = CdReadFile( (char *)(overlayFile), &load_all_overlays_here, 0);
2021-04-29 19:05:54 +02:00
2021-04-29 18:46:10 +02:00
CdReadSync(0, 0);
#endif
}
// Pad button timer
while ( timer > 0 ) {
timer --;
}
2021-05-04 17:26:44 +02:00
// SIO FUN : USELESS as it hijacks unirom's SIO implementation... Keeping it for ref
//~ static char * ok = "OKAY";
//~ static char * buffer = " ";
//static u_char clearFlag = 1;
//~ if( SIO ){
//~ // Is SIO is not init, dot it
//~ if( ! SIOinit ){
//~ ResetCallback();
//~ AddSIO(115200);
//~ ResetGraph(0);
//~ SIO_CLEAR;
//~ SIOinit = 1;
//~ }
//if ( clearFlag ){
//SIO_CLEAR;
//clearFlag = 0;
//}
//~ if( strlen(buffer) > 4){
//~ memmove(buffer, buffer + 1, strlen(buffer));
//~ }
//~ // Clears driver status error-related bits
//~ // Check if sio driver is able to write communications data
//~ if( SIO_STATUS & SR_RXRDY ){
//~ // Read byte
//~ char c = SIO_READB;
//~ // Add to buffer
//~ strncat(buffer, &c, 1);
//~ }
//~ // Compare buffer to string
//~ if( strcmp(ok, buffer) == 0) {
//~ loadFileID = !loadFileID;
//~ DelSIO();
//~ SIO = 0;
//~ SIOinit = 0;
//clearFlag = 1;
//~ }
//~ if( buffer ){
//~ FntPrint("%s", buffer);
//~ }
//~ }
// END SIO FUN
2021-04-29 19:05:54 +02:00
// Read pad status
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
PadStatus = PadRead(0);
2021-04-29 18:46:10 +02:00
// If select is pressed, change overlay
if (PadStatus & PADselect && !timer) {
// We send the memory address where the file should be loaded, the memory address of the loadFileID, so that the screen is updated when it changes, and the file id.
2021-04-30 16:42:24 +02:00
// format : 00(:)01(:)06(:)04 xx xx xx xx(:)04 xx xx xx xx(:)01 (separators are not send)
// 14 bytes
2021-05-01 14:51:25 +02:00
2021-04-30 16:42:24 +02:00
PCload( &load_all_overlays_here, &loadFileID, overlayFileID );
2021-05-01 14:51:25 +02:00
2021-05-04 17:26:44 +02:00
//~ SIO = 1;
2021-04-29 18:46:10 +02:00
#ifdef USECD
// We can do that because we only have two files
loadFileID = !loadFileID;
#endif
timer = 30;
}
2021-04-29 19:05:54 +02:00
if (AutoRotate) {
Rotate.vy += 8; // Pan
Rotate.vx += 8; // Tilt
}
// Clear the current OT
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
ClearOTagR(ot[db], OTLEN);
// Convert and set the matrixes
2021-04-29 18:46:10 +02:00
RotMatrix(&Rotate, &Matrix);
2021-04-29 19:05:54 +02:00
2021-04-29 18:46:10 +02:00
TransMatrix(&Matrix, &Trans);
2021-04-29 19:05:54 +02:00
2021-04-29 18:46:10 +02:00
ScaleMatrix(&Matrix, &Scale);
2021-04-29 19:05:54 +02:00
SetRotMatrix(&Matrix);
2021-04-29 18:46:10 +02:00
SetTransMatrix(&Matrix);
2021-04-29 19:05:54 +02:00
// Render the sample vector model
2021-04-30 16:42:24 +02:00
2021-04-29 19:05:54 +02:00
t=0;
2021-04-29 18:46:10 +02:00
// modelCube is a TMESH, len member == # vertices, but here it's # of triangle... So, for each tri * 3 vertices ...
2021-04-29 19:05:54 +02:00
for (i = 0; i < (model->tmesh->len*3); i += 3) {
2021-04-29 18:46:10 +02:00
poly = (POLY_GT3 *)nextpri;
2021-04-29 19:05:54 +02:00
2021-04-29 18:46:10 +02:00
// Initialize the primitive and set its color values
2021-04-29 19:05:54 +02:00
2021-04-29 18:46:10 +02:00
SetPolyGT3(poly);
((POLY_GT3 *)poly)->tpage = getTPage(tim_cube.mode&0x3, 0,
tim_cube.prect->x,
tim_cube.prect->y
);
2021-04-29 19:05:54 +02:00
setRGB0(poly, model->tmesh->c[i].r , model->tmesh->c[i].g , model->tmesh->c[i].b);
setRGB1(poly, model->tmesh->c[i+1].r, model->tmesh->c[i+1].g, model->tmesh->c[i+1].b);
setRGB2(poly, model->tmesh->c[i+2].r, model->tmesh->c[i+2].g, model->tmesh->c[i+2].b);
2021-04-29 18:46:10 +02:00
setUV3(poly, model->tmesh->u[i].vx , model->tmesh->u[i].vy,
model->tmesh->u[i+1].vx, model->tmesh->u[i+1].vy,
model->tmesh->u[i+2].vx, model->tmesh->u[i+2].vy);
// Rotate, translate, and project the vectors and output the results into a primitive
OTz = RotTransPers(&model->tmesh->v[model->index[t]] , (long*)&poly->x0, &p, &Flag);
2021-04-29 19:05:54 +02:00
OTz += RotTransPers(&model->tmesh->v[model->index[t+1]], (long*)&poly->x1, &p, &Flag);
OTz += RotTransPers(&model->tmesh->v[model->index[t+2]], (long*)&poly->x2, &p, &Flag);
// Sort the primitive into the OT
OTz /= 3;
if ((OTz > 0) && (OTz < OTLEN))
AddPrim(&ot[db][OTz-2], poly);
nextpri += sizeof(POLY_GT3);
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
t+=3;
}
2021-04-29 18:46:10 +02:00
dr_mode = (DR_MODE *)nextpri;
setDrawMode(dr_mode,1,0, getTPage(tim_cube.mode&0x3, 0,
tim_cube.prect->x,
tim_cube.prect->y), &tws); //set texture window
AddPrim(&ot[db], dr_mode);
nextpri += sizeof(DR_MODE);
2021-04-29 19:05:54 +02:00
FntPrint("Hello overlay!\n");
2021-04-29 18:46:10 +02:00
#ifndef USECD
2021-04-30 16:42:24 +02:00
2021-05-01 14:51:25 +02:00
FntPrint("Overlay with id %d loaded at 0x%08x\n%x", overlayFileID, &load_all_overlays_here, &overlayFileID );
2021-04-29 18:46:10 +02:00
2021-04-30 16:42:24 +02:00
#endif
2021-04-29 18:46:10 +02:00
#ifdef USECD
FntPrint("File: %s\n", overlayFile);
FntPrint("Bytes read: %d", cdread);
#endif
FntFlush(-1);
2021-04-29 19:05:54 +02:00
display();
2021-04-29 18:46:10 +02:00
2021-04-29 19:05:54 +02:00
}
2021-04-29 18:46:10 +02:00
return 0;
}