This commit is contained in:
2025-08-02 12:10:59 -04:00
parent 0a4dd81318
commit 5bb19672a7
11 changed files with 1227 additions and 32 deletions
+14 -15
View File
@@ -20,12 +20,12 @@ function main(args)
os.exit(1)
end
print(string.format("Input file: %s", args[1]))
print(string.format("Output file: %s", args[2]))
-- print(string.format("Input file: %s", args[1]))
-- print(string.format("Output file: %s", args[2]))
-- PS1 executables have a maximum size limit of 2MB
local max_size = 0x200000
print(string.format("\nChecking input file size (max: %d bytes)...", max_size))
-- print(string.format("\nChecking input file size (max: %d bytes)...", max_size))
local infile_size = file_size(args[1])
if not infile_size then
@@ -33,14 +33,14 @@ function main(args)
os.exit(1)
end
print(string.format("Input file size: %d bytes", infile_size))
-- print(string.format("Input file size: %d bytes", infile_size))
if infile_size > max_size then
io.stderr:write(string.format("Error: Input file %s longer than %d bytes\n", args[1], max_size))
os.exit(1)
end
print("\nOpening files...")
-- print("\nOpening files...")
local ofile = io.open(args[2], "wb")
if not ofile then
io.stderr:write("Error: Cannot open output file " .. args[2] .. "\n")
@@ -53,16 +53,15 @@ function main(args)
os.exit(1)
end
print("Writing PS-X executable header...")
-- PS1 executables start with "PS-X EXE" magic string
-- print("Writing PS-X executable header...")
ofile:write("PS-X EXE")
-- Write entry point address (where the PS1 will jump to start execution)
-- 0x80010000 is a standard entry point in PS1 RAM
ofile:seek("set", 0x10)
ofile:write(string.pack("<I4", 0x80010000))
print(" Entry point: 0x80010000")
-- print(" Entry point: 0x80010000")
-- Initial GP/R28 register value (Global Pointer for data addressing)
-- 0xFFFFFFFF means it will be set by crt0.S startup code
@@ -70,21 +69,21 @@ function main(args)
-- Destination address in RAM where the executable will be loaded
ofile:write(string.pack("<I4", 0x80010000))
print(" Load address: 0x80010000")
-- print(" Load address: 0x80010000")
-- Initial stack pointer (SP/R29) and frame pointer (FP/R30)
-- 0x801FFF00 points near the top of the 2MB main RAM
ofile:seek("set", 0x30)
ofile:write(string.pack("<I4", 0x801FFF00))
print(" Stack pointer: 0x801FFF00")
-- print(" Stack pointer: 0x801FFF00")
-- PS1 executables have an 0x800 (2048) byte header
-- Zero fill the rest of the header
ofile:seek("set", 0x800)
print(" Header padding complete (2048 bytes)")
-- print(" Header padding complete (2048 bytes)")
-- Copy the actual program binary data after the header
print("\nCopying program data...")
-- print("\nCopying program data...")
local buffer_size = 0x2000 -- 8KB chunks for efficient copying
local bytes_copied = 0
@@ -118,13 +117,13 @@ function main(args)
-- This goes at offset 0x1C in the header
ofile:seek("set", 0x1C)
ofile:write(string.pack("<I4", exe_size - 0x800))
print(string.format("\nProgram size field set to: %d bytes", exe_size - 0x800))
-- print(string.format("\nProgram size field set to: %d bytes", exe_size - 0x800))
ifile:close()
ofile:close()
print(string.format("\nSuccess! PS1 executable created: %s", args[2]))
print(string.format("Total file size: %d bytes", exe_size))
-- print(string.format("\nSuccess! PS1 executable created: %s", args[2]))
print(string.format("Total file size: %d bytes\n", exe_size))
end
-- Run main with command line arguments