From 1dc00c3661cd79d0efb3eccfea5e6014f82d6f59 Mon Sep 17 00:00:00 2001 From: ABelliqueux Date: Sat, 17 Jul 2021 13:16:05 +0200 Subject: [PATCH] Add CD file loading example --- Makefile | 13 ++--- README.md | 7 +++ hello_cd/HELO.DAT | 2 + hello_cd/Makefile | 12 +++++ hello_cd/README.md | 15 ++++++ hello_cd/hello_cd.c | 118 +++++++++++++++++++++++++++++++++++++++++ hello_cd/isoconfig.xml | 100 ++++++++++++++++++++++++++++++++++ hello_cd/system.cnf | 4 ++ 8 files changed, 263 insertions(+), 8 deletions(-) create mode 100644 hello_cd/HELO.DAT create mode 100644 hello_cd/Makefile create mode 100644 hello_cd/README.md create mode 100644 hello_cd/hello_cd.c create mode 100644 hello_cd/isoconfig.xml create mode 100644 hello_cd/system.cnf diff --git a/Makefile b/Makefile index eaa7b5e..2b2ccb1 100644 --- a/Makefile +++ b/Makefile @@ -26,10 +26,6 @@ hello_poly_gt_tw: $(MAKE) -C hello_poly_gt_tw hello_poly_inline: $(MAKE) -C hello_poly_inline -hello_poly_subdiv: - $(MAKE) -C hello_poly_subdiv -hello_rsd: - $(MAKE) -C hello_rsd hello_sio: $(MAKE) -C hello_sio hello_sprt: @@ -38,6 +34,8 @@ hello_tile: $(MAKE) -C hello_tile hello_vag: $(MAKE) -C hello_vag +hello_cd: + $(MAKE) -C hello_cd all hello_cdda: $(MAKE) -C hello_cdda @@ -55,7 +53,6 @@ clean: $(MAKE) -C hello_poly_gt clean $(MAKE) -C hello_poly_gt_tw clean $(MAKE) -C hello_poly_inline clean - $(MAKE) -C hello_poly_subdiv clean $(MAKE) -C hello_rsd clean $(MAKE) -C hello_sio clean $(MAKE) -C hello_sprt clean @@ -63,6 +60,7 @@ clean: $(MAKE) -C hello_vag clean $(MAKE) -C hello_world clean $(MAKE) -C hello_cdda clean + $(MAKE) -C hello_cd cleansub all: $(MAKE) -C hello_2pads @@ -78,15 +76,14 @@ all: $(MAKE) -C hello_poly_gt $(MAKE) -C hello_poly_gt_tw $(MAKE) -C hello_poly_inline - $(MAKE) -C hello_poly_subdiv - $(MAKE) -C hello_rsd $(MAKE) -C hello_sio $(MAKE) -C hello_sprt $(MAKE) -C hello_tile $(MAKE) -C hello_vag $(MAKE) -C hello_world + $(MAKE) -C hello_cd all $(MAKE) -C hello_cdda # declare phony rules -.PHONY: hello_2pads hello_cube hello_cubetex hello_poly_fun hello_gte_opti hello_light hello_multivag hello_pad hello_poly hello_poly_ft hello_poly_gt hello_poly_gt_tw hello_poly_inline hello_poly_subdiv hello_rsd hello_sio hello_sprt hello_tile hello_vag hello_world hello_cdda \ +.PHONY: hello_2pads hello_cube hello_cubetex hello_poly_fun hello_gte_opti hello_light hello_multivag hello_pad hello_poly hello_poly_ft hello_poly_gt hello_poly_gt_tw hello_poly_inline hello_sio hello_sprt hello_tile hello_vag hello_world hello_cdda hello_cd\ clean all diff --git a/README.md b/README.md index e063990..7cdba2a 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,13 @@ Alternatively, you can use `make example_name` to only build that example, i.e : If you want to remove all the files generated by the compilation process, type `make clean`. +## Upcoming examples + + * hello_xa (audio) + * hello_str (video) + * hello_poly_subdiv (polygon subdivision) + * hello_rsd (rsd format) + # Links and Doc * [Getting started with PSX dev](https://psx.arthus.net/starting.html) diff --git a/hello_cd/HELO.DAT b/hello_cd/HELO.DAT new file mode 100644 index 0000000..70df38a --- /dev/null +++ b/hello_cd/HELO.DAT @@ -0,0 +1,2 @@ +hello cd ! This is the content of the +HELO.DAT file. diff --git a/hello_cd/Makefile b/hello_cd/Makefile new file mode 100644 index 0000000..6b99d0f --- /dev/null +++ b/hello_cd/Makefile @@ -0,0 +1,12 @@ +.PHONY: all cleansub +all: + mkpsxiso -y ./isoconfig.xml +cleansub: + $(MAKE) clean + rm -f hello_cd.cue hello_cd.bin + +TARGET = hello_cd + +SRCS = hello_cd.c \ + +include ../common.mk diff --git a/hello_cd/README.md b/hello_cd/README.md new file mode 100644 index 0000000..6a31b99 --- /dev/null +++ b/hello_cd/README.md @@ -0,0 +1,15 @@ +## Loading a file from CD + +You need [mkpsxiso](https://github.com/Lameguy64/mkpsxiso) in your $PATH to generate a PSX disk image. + +### Compile + +```bash +make all +``` + +### Clean directory + +```bash +make cleansub +``` diff --git a/hello_cd/hello_cd.c b/hello_cd/hello_cd.c new file mode 100644 index 0000000..fc2362f --- /dev/null +++ b/hello_cd/hello_cd.c @@ -0,0 +1,118 @@ +// Load files from CD and execute them +// Schnappy 07-2021 +// based on Lameguy64's tutorial : http://lameguy64.net/svn/pstutorials/chapter1/1-display.html +#include +#include +#include +#include +#include +#include +// CD library +#include +#include + + +#define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL +#define SCREENXRES 320 // Screen width +#define SCREENYRES 240 + (VMODE << 4) // Screen height : If VMODE is 0 = 240, if VMODE is 1 = 256 +#define CENTERX SCREENXRES/2 // Center of screen on x +#define CENTERY SCREENYRES/2 // Center of screen on y +#define MARGINX 0 // margins for text display +#define MARGINY 32 +#define FONTSIZE 8 * 7 // Text Field Height +DISPENV disp[2]; // Double buffered DISPENV and DRAWENV +DRAWENV draw[2]; +short db = 0; // index of which buffer is used, values 0, 1 + +// CD specifics +#define CD_SECTOR_SIZE 2048 +// Converting bytes to sectors SECTOR_SIZE is defined in words, aka int +#define BtoS(len) ( ( len + CD_SECTOR_SIZE - 1 ) / CD_SECTOR_SIZE ) +// Name of file to load +static char * exeFile; +// libcd's CD file structure contains size, location and filename +CdlFILE filePos = {0}; +//~ struct EXEC * exeStruct; +// Define start address of allocated memory +// Let's use an array so we don't have to worry about using a memory segment that's already in use. +static unsigned char ramAddr[0x40000]; // https://discord.com/channels/642647820683444236/663664210525290507/864936962199781387 +// We could also set a memory address manually, but we have to make sure this won't get in the way of other routines. +// void * ramAddr = (void *)0x80030D40; +// Load data to this buffer +u_long * dataBuffer; +// Those are not strictly needed, but we'll use them to see the commands results. +// They could be replaced by a 0 in the various functions they're used with. +u_char CtrlResult[8]; +// Value returned by CDread() - 1 is good, 0 is bad +int CDreadOK = 0; +// Value returned by CDsync() - Returns remaining sectors to load. 0 is good. +int CDreadResult = 0; + +void init(void) +{ + ResetGraph(0); // Initialize drawing engine with a complete reset (0) + SetDefDispEnv(&disp[0], 0, 0 , SCREENXRES, SCREENYRES); // Set display area for both &disp[0] and &disp[1] + SetDefDispEnv(&disp[1], 0, SCREENYRES, SCREENXRES, SCREENYRES); // &disp[0] is on top of &disp[1] + SetDefDrawEnv(&draw[0], 0, SCREENYRES, SCREENXRES, SCREENYRES); // Set draw for both &draw[0] and &draw[1] + SetDefDrawEnv(&draw[1], 0, 0 , SCREENXRES, SCREENYRES); // &draw[0] is below &draw[1] + // Set video mode + if (VMODE){ SetVideoMode(MODE_PAL);} + SetDispMask(1); // Display on screen + setRGB0(&draw[0], 255, 50, 50); // set color for first draw area + setRGB0(&draw[1], 255, 50, 50); // set color for second draw area + draw[0].isbg = 1; // set mask for draw areas. 1 means repainting the area with the RGB color each frame + draw[1].isbg = 1; + PutDispEnv(&disp[db]); // set the disp and draw environnments + PutDrawEnv(&draw[db]); + FntLoad(960, 0); // Load font to vram at 960,0(+128) + FntOpen(MARGINX, SCREENYRES - MARGINY - FONTSIZE, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); // FntOpen(x, y, width, height, black_bg, max. nbr. chars +} +void display(void) +{ + DrawSync(0); // Wait for all drawing to terminate + VSync(0); // Wait for the next vertical blank + PutDispEnv(&disp[db]); // set alternate disp and draw environnments + PutDrawEnv(&draw[db]); + db = !db; // flip db value (0 or 1) +} +int main(void) +{ + // Init display + init(); + // Init CD system + CdInit(); + // Init heap + InitHeap((u_long *)ramAddr, sizeof(ramAddr)); + // If the other method was chosen at l.39 + // InitHeap((void *)0x80030D40, 0x40000); + // Set name of file to load + exeFile = "\\HELO.DAT;1"; + // Get file position from filename + CdSearchFile( &filePos, exeFile); + // Allocate memory + dataBuffer = malloc( BtoS(filePos.size) * CD_SECTOR_SIZE ); + // Issue CdlSetloc CDROM command : Set the seek target position + // Beware of a misnomed 'sector' member in the CdlLOC struct that should really be named 'frame'. + // https://discord.com/channels/642647820683444236/663664210525290507/864912470996942910 + CdControl(CdlSetloc, (u_char *)&filePos.pos, CtrlResult); + // Read data and load it to dataBuffer + CDreadOK = CdRead( (int)BtoS(filePos.size), (u_long *)dataBuffer, CdlModeSpeed ); + // Wait for operation to complete + CDreadResult = CdReadSync(0, 0); + + while (1) // infinite loop + { + // Print the content of the loaded file - See HELO.DAT + FntPrint("%s%d\n", (char *)dataBuffer, VSync(-1)); + // Print heap and buffer addresses + FntPrint("Heap: %x - Buf: %x\n", ramAddr, dataBuffer); + // Print returned values + FntPrint("CdCtrl: %d\nRead : %d %d\n", CtrlResult[0], CDreadOK, CDreadResult); + // Print filesize in bytes/sectors + FntPrint("Size: %dB sectors: %d", filePos.size, BtoS(filePos.size)); + + FntFlush(-1); // Draw print stream + display(); // Execute display() + } + return 0; + } diff --git a/hello_cd/isoconfig.xml b/hello_cd/isoconfig.xml new file mode 100644 index 0000000..68d852f --- /dev/null +++ b/hello_cd/isoconfig.xml @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hello_cd/system.cnf b/hello_cd/system.cnf new file mode 100644 index 0000000..ae2db18 --- /dev/null +++ b/hello_cd/system.cnf @@ -0,0 +1,4 @@ +BOOT=cdrom:\SCES_313.37;1 +TCB=4 +EVENT=10 +STACK=801FFFF0