PS-X OS
Kernel of the PlayStation that provides its services as C functions.
| RAM | 64K bytes (0x00000000 to 0x0000FFFF) |
|---|---|
| Location | OS ROM (BIOS) |
| Library | libapi (kernel.h, libapi.h) |
Overview
The PS-X OS was developed for the R3000, which is the CPU of the PlayStation. Its design concept is to give the developer of game programs an environment in which programs can be controlled with interrupts. Based on this concept, the kernel is provided as the aggregate of the services (subroutines) that control the R3000 and the PlayStation hardware, and each service is provided as a C language function.12 The kernel is stored in the 512KB OS ROM, which programmers call the BIOS.3
The R3000 interrupt processing is complicated, so the OS uses a substitute dispatcher with a simple interface and low overhead.2 To place emphasis on application performance, the OS uses at most 64K bytes of RAM and as little CPU time as possible. The OS system tables are disclosed so that applications can extend the OS and read its internal data.12 For greater speed the OS also leaves out the checks of prohibited operations that other operating systems make. Consequently the application must perform these checks itself, and careful programming makes higher tuning possible.12
The OS is an environment for game program developers. It therefore has no interface for the user to operate the hardware directly, apart from the debug monitor in the debugging environment, and the application must provide the user interface.45
Tasks
The PS-X OS is a multi-tasking operating system that can carry out several processes asynchronously. Multi-task control suits the control of the CD-ROM drive, a comparatively slow device, and the playing of background music.6 Immediately after the OS starts, however, it is in single task mode, and a program that does not start more threads stays in single task mode. A programmer does not have to use the multi-tasking feature at all.67 The threads page describes thread management.
File System
The file system of the PS-X OS takes as a premise that files are on CD-ROM, the medium on which applications are supplied. It is based on ISO 9660 level 1, which is an international standard.8 The file system is accessed through a device driver, which allows several file systems to coexist and spares the application low-level file handling.6 A file name is an 8-character base name and a 3-character extension separated by a period, the same format as MS-DOS, and 1-byte katakana and kanji cannot be used.8 The file I/O page describes the devices and the file functions.
Memory Map
The space that a program uses is called logic space. It is expressed by a 32-bit address and mapped onto physical space several times, and access to a logic address with no physical memory behind it raises a bus error.89 The interface registers of the peripheral devices are also mapped in logic space as memory mapped I/O. Memory may be accessed in units of 1 to 4 bytes, but an interface register must be accessed in its own unit size, and any other size raises a bus error.9 The memory map page lists the segments.
The kernel uses the first 64K bytes of memory, and the addresses that the user may use begin at 0x00010000.10 Logic addresses from 0x80000000 run through the I-cache and addresses from 0xA0000000 do not. Sony therefore recommends placing programs in segment B from 0x80000000.11
Library Components
The PlayStation libraries form two levels according to their relation to the OS. A program may use either level as needed, and with some exceptions may use both at the same time.12 The libraries are summarized below.131415
| Library | Name | Contents |
|---|---|---|
| libapi | Kernel Library | Interface (API) between the PlayStation OS and applications |
| libc, libc2 | Standard C Libraries | Subset of the standard C library |
| libmath | Math Library | ANSI/IEEE754 math functions and software floating point |
| libcard | Memory Card Library | Memory Card, its file system and drivers |
| libmcrd | Extended Memory Card Library | High-level interface to the Memory Card |
| libpress | Data Compression Library | Compression and expansion of image and sound data |
| libgpu | Basic Graphics Library | Drawing engine commands and drawing command lists |
| libgte | Basic Geometry Library | Control of the GTE, matrices and vertices |
| libgs | Extended Graphics Library | Three-dimensional graphics system on libgpu and libgte |
| libcd | CD/Streaming Library | CD-ROM data, DA and XA playback, streaming |
| libds | Extended CD-ROM Library | libcd functions with error recovery |
| libetc | Peripherals Library | Callbacks for controllers and low-level interrupts |
| libtap | Multi Tap Library | 3 to 8 controllers and Memory Cards through the Multi Tap |
| libgun | Gun Library | Light pen type input on the controller connector |
| libpad | Controller Library | Controllers, including extended protocol controllers such as DUAL SHOCK |
| libcomb | Link Cable Library | Communication over the link cable |
| libsnd | Extended Sound Library | Sequence playback from score data |
| libspu | Basic Sound Library | Control of the SPU |
| libsio | Serial Input/Output Library | Standard I/O on SIO 1 of the debugging station |
| libhmd | HMD Library | HMD format of modeling, animation, texture and MIMe data |
| libmcx | PDA Library | PDA in a Memory Card slot |
| mcgui | Memory Card GUI module | Loading and saving with a user interface |
The PlayStation Operating System manual gives a shorter list of libgpu, libgte, libgs, libpress, libstr, libsnd, libapi and libetc, and it describes libgpu as "CPU basic operation".16
Services
The services of the PS-X OS are called application programmer interfaces (API), and a program that calls the OS must link their function libraries. All PlayStation features are covered by the API.17 Programs that use the kernel services link libapi.lib and include libapi.h and kernel.h.18 The API has the following services.1716
| Service | Content |
|---|---|
| Module Management Service | Loads and executes user application programs |
| Event Management Service | Controls program execution triggered by asynchronous events |
| Controller Service | Supports up to 64 controllers of up to 16 buttons each |
| I/O Management Service | Low-level input and output to files and logic devices |
| Thread Management Service | Threads of register states, stack areas and CPU states |
| Other Services | I-cache and R3000 setup, permission and prohibition of interrupts |
| Standard C Library | K&R standard C functions such as string operations and standard I/O |
| Root Counter Management Service | Counting for time restrictions and timing adjustments |
All file control on the PlayStation is performed with the I/O management service.17 The function tables are on the BIOS page.
Descriptors
When a program works with system resources such as files or threads, the kernel provides descriptors to refer to them. A descriptor is an unsigned 32-bit integer.19
| Bit Number | Contents |
|---|---|
| 31 to 24 | Descriptor classification |
| 23 to 16 | Reserved by system |
| 15 to 0 | System table number |
The classifications are defined as macros in kernel.h.19
| Macro | Class contents | Notes |
|---|---|---|
| DescTH | Thread | |
| DescHW | Hardware | System internal use |
| DescEV | Event | |
| DescRC | Root counter | |
| DescUEV | User-defined flag | |
| DescSW | System call | System internal use |
A program obtains a descriptor from the Open() function of each resource, uses it to specify the resource in every operation, and releases it with the matching Close() function after use.19 For example, the default thread has the descriptor DescTH|0x0000 = 0xFF000000.20
Critical Sections and Interrupt Context
Any function that modifies data within the kernel must be executed with interrupts inhibited, and such a section of code is called a critical section. Interrupts are inhibited immediately after the PlayStation starts until ResetCallbacks() enables them, after EnterCriticalSection() until ExitCriticalSection(), and at the start of an event handler.21 The syscalls page describes these functions.
The normal execution of a program is called its main flow. When an interrupt or exception occurs, the kernel saves the registers in the execution TCB as the main flow context, and the state after saving is called the interrupt context. Processing then begins at 0x00000080, which jumps to the kernel interrupt dispatcher, and when the interrupt processing is complete the main flow registers are restored.21 Event handlers execute in interrupt context on the interrupt stack, and callbacks on the callback stack declared in libetc.21
Code in interrupt context must finish in the shortest possible time, because halting interrupts for long may adversely affect operation. Functions that generate internal exceptions, such as ExitCriticalSection(), destroy the main flow context, which the thread management service can prevent by changing the execution TCB. ReturnFromException() inside an event handler returns to the main flow but breaks off the interrupt dispatcher, and Sony therefore restricts it to error management.21
Code in interrupt context must not call functions that use internal interrupts, because those functions cannot complete while interrupts are not generated. It must not call non-reentrant functions that the main flow may also call, and most library functions, including the kernel services, are not reentrant. A callback function must not call ReturnFromException().10
Callbacks
Libraries that handle devices with DMA, such as libgpu and libsnd, have functions that register callback functions in the kernel. A callback is called automatically when a DMA transfer is completed, and it runs in callback context on the callback stack. The program can complete the transfer processing by setting a flag in an external variable and issuing an event.21
Root Counters
The root counter management service provides counting for time restrictions and timing adjustments in game programs.1610 A counter counts up from zero to a 16-bit target value. At the target it generates an interrupt (each counter can be masked), clears to zero and starts counting again. The values read by polling therefore run from 0 to the target value minus 1.10 The vertical synchronization counter has its target fixed at 1 and therefore generates an interrupt at each vertical blank.10 The counters are named by macros.10
| Macro | Root counter (Run-Time Library Overview) | Counter (PlayStation Operating System) | Notes |
|---|---|---|---|
| RCntCNT0 | Display pixels | ||
| RCntCNT1 | System clock | Horizontal synchronization | Target value more than 2 |
| RCntCNT2 | System clock (8 spaces) | System clock | Target value more than 2 |
| RCntCNT3 | Vertical synch | Vertical synchronization | Target fixed at 1, halting count is invalid |
The two Sony manuals describe the counters differently, and the table gives both.1016 One tick of the CPU system clock is about 0.03 microseconds. The counts for the graphics display have the following timings.22
| Counter Event | NTSC | PAL | Unit |
|---|---|---|---|
| Vertical Sync | 1/60 | 1/50 | Second |
| Horizontal Sync | 63.56 | 64.00 | Microsecond |
| Pixel Display | N x 0.0186243 | N x 0.01879578 | Microsecond |
| Display Width | N |
|---|---|
| 256 pixels | 10 |
| 320 | 8 |
| 384 | 7 |
| 512 | 5 |
| 640 | 4 |
The root counter uses the hardware counting function. Counting therefore continues while interrupts are disabled, and StopRCnt() does not stop counting: it only stops the counter from generating interrupts, and StartRCnt() only allows them.22 The mode of each counter is set by OR-ing the macros below.22
| Macro | Counter | Contents |
|---|---|---|
| RCntMdINTR | All | Interrupt permitted |
| RCntMdNOINTR | All | Interrupt prohibited (polling only) |
| RCntMdSP (default) | RCntCNT0, RCntCNT1 | Use prohibited |
| RCntMdSP (default) | RCntCNT2 | System clock 8 second cycle |
| RCntMdSP (default) | RCntCNT3 | Vertical blanking |
| RCntMdSC | RCntCNT0, RCntCNT1 | System clock |
| RCntMdFR (default) | All | Normal count |
| RCntMdGATE | All | Gate condition valid |
With RCntMdGATE a counter counts only while its gate condition holds. RCntCNT0 does not count during horizontal blanking, RCntCNT1 does not count during vertical blanking, and RCntCNT2 and RCntCNT3 have no gate condition.23
All counters are stopped or free running immediately after the kernel starts, so a counter must always be initialized before use. A counter interrupt cannot occur within a critical section.23 The kernel itself uses root counter 3 to read the controller buttons, and the buttons cannot be read while root counter 3 is stopped or uninitialized.23
The root counter and the event management service together detect the start of the vertical blanking period. The program opens an event on RCntCNT3, sets the counter with target 1, and starts it. The sample below shows the calls.24
Sample()
{
unsigned long EV;
EnterCriticalSection();
EV = OpenEvent(RCntCNT3, EvSpINT, EvMdNOINTR, NULL);
EnableEvent(EV);
SetRCnt(RCntCNT3, 1, RCntMdINTR);
StartRCnt(RCntCNT3);
/* initialization of controller */
while (1) {
WaitEvent(EV);
/* per-frame processing */
}
}
Controller Features
libapi provides a low-level interface to the controllers. The application processes the received data directly, and the type of each controller is identified dynamically.25 InitPAD() registers two receive buffers and their maximum lengths, and StartPAD() begins reading the controllers on each vertical blank interrupt. The presence of a controller and its state are then found by testing the buffer contents.25
The first byte of a buffer is the receive result, which is 0x00 for success. The upper 4 bits of the second byte are the terminal type, and the lower 4 bits are half the number of data bytes that follow from the third byte on.2627 The terminal types are shown below.26
| Terminal Type | Controller Name | Main Controller Model Number |
|---|---|---|
| 1 | Mouse | SCPH-1030 |
| 2 | 16-Button Analog | SLPH-00001 (Namco) |
| 3 | Gun Controller | SLPH-00014 (Konami) |
| 4 | 16-Button | SCPH-1080, SCPH-1150 |
| 5 | Analog Joystick | SCPH-1110 |
| 6 | Gun Controller | SLPH-00034 (Namco) |
| 7 | Analog Controller | SCPH-1150 |
| 8 | Multi Tap | SCPH-1070 |
Bytes 2 and 3 hold the button status, in which a bit is 1 for released and 0 for pushed. For the 16-button controller the bits of the button status are assigned as shown below.27
| Bit | D15 | D14 | D13 | D12 | D11 | D10 | D9 | D8 | D3 | D2 | D1 | D0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Button | Left | Down | Right | Up | START | SELECT | R1 | L1 | R2 | L2 |
The table leaves out bits D7 to D4, which hold the four symbol buttons.27
The Analog Controller adds R3 at D10 and L3 at D9. The analog devices report stick positions from 0 through 128 to 255 in bytes 4 to 7, and the mouse reports X and Y movement from -128 to 127 in bytes 4 and 5.262827
Memory Allocation
There are three systems of memory allocation. InitHeap() and the libc malloc(), calloc(), realloc() and free() call the ROM routines. InitHeap2() and malloc2() are a RAM-based version, and InitHeap3() and malloc3() are a faster RAM-based version added in Library 4.0.2930 The ROM malloc has a bug in which an area cannot be completely released by free(). Because the ROM cannot be corrected, the bug was fixed in malloc2 and the ROM version was left for compatibility.30 All three allocate blocks by first fit.30
The C library heap functions are part of the OS, but the OS does not initialize the heap, because the game program developer manages all available memory. A program first calls the heap initialization function with the start address and size of the heap.24 InitHeap() must be executed in a critical section, and some of the given size is used for overhead.31 InitHeap3() takes a size that is a multiple of 8 bytes and discards any remainder.32
Programming Notes
Overlay processing takes the form of executing a child process from a resident module. The OS drives the hardware and starts the file to be executed, but for an overlay the application must do this.7 The PlayStation has no character generator and provides no fonts for text display, so the application must draw character strings as part of its graphics.7 The kernel ROM does contain a kanji font. The BIOS page describes it.33
The entry point of an execution file may be any function given at link time. When none is given, main() is used, and the entry function cannot take an argument.24 A program needs a stack pointer value as well as a start address, because a stack pointer that holds an address that cannot be accessed causes a bus error.34
Immediately after starting, standard output is allocated to file descriptor 0 and standard input to file descriptor 1 on RS232C channel 0 (device name tty00). The port runs at 8-bit characters, 1 stop bit, no parity and hardware flow control.34 The Run-Time Library Overview instead assigns the standard I/O stream to a NULL device on the game unit and standard output to Debug Message Window #0 in the development environment.35
The scratchpad (the S segment) is the fastest memory for data. Sony advises placing often-used arrays, global variables and functions there, using -O2 or -O3 for the higher I-cache hit rate, and keeping C functions to 4 or fewer arguments so that all are passed in registers.2436 A transfer between registers takes 1 cycle and an access to the scratchpad also takes 1 cycle. An access to data in main memory takes 5 cycles.37
References
-
^ a b c Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 1-3.
-
^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 1-3.
-
^ Sony Computer Entertainment Inc. PlayStation Hardware. August 1998. Page 2-3.
-
^ Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 2-3.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 1-4.
-
^ a b c Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 1-4.
-
^ a b c Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 4-3.
-
^ a b c Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 2-4.
-
^ a b Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 2-5, Table 2-2.
-
^ a b c d e f g Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-7, Table 2-5.
-
^ Sony Computer Entertainment. PlayStation Development FAQ, OS Conference. Release v1.2, 29 February 1996. Item 1.2.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 1-5, Figure 1-2.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 1-6.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 1-7.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 1-8.
-
^ a b c d Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 3-4, Table 3-1.
-
^ a b c Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 3-3.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-3.
-
^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-5, Table 2-3, Table 2-4.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-12.
-
^ a b c d e Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-6.
-
^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-8, Tables 2-6 to 2-10.
-
^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-9, Table 2-11.
-
^ a b c d Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 4-4.
-
^ a b Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-17.
-
^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-18, Tables 2-20 to 2-24.
-
^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-20, Table 2-29.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-19, Tables 2-25 to 2-27.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-21.
-
^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-22, Table 2-32.
-
^ Sony Computer Entertainment Inc. Run-Time Library Reference. January 1999. Page 1-39, InitHeap.
-
^ Sony Computer Entertainment Inc. Run-Time Library Reference. January 1999. Page 1-41, InitHeap3.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-20, Table 2-30.
-
^ a b Sony Computer Entertainment Inc. PlayStation Operating System. August 1998. Page 4-5.
-
^ Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 2-16.
-
^ Sony Computer Entertainment. PlayStation Development FAQ, OS Conference. Release v1.2, 29 February 1996. Item 1.5.
-
^ Sony Computer Entertainment. PlayStation Development FAQ, OS Conference. Release v1.2, 29 February 1996. Item 1.6.