Commodore‎ > ‎C128‎ > ‎

N-Progs

The C128 has plenty of RAM (for an 8-bit PC) to allow for multiple programs. This is rarely taken advantage of; the most popular way to do this is with GEOS but it is an entire operating system that renders standard (non-GEOS) software useless!

On the Commodore 128 Alive! forums a challange was posted by airship to write a utiltity that would allow you to switch between multiple programs. Well this could be a very tall task depending on how far you want to take it. But I decided to do it in a minimal way just to say it has been done. So without further ado, I humbly present...

 
  Original Games  

N-Progs is a set of ML programs for the Commodore 128 that allow multiple BASIC programs to exist in memory at the same time. The download contains a README, the program files as seperate binary files, and all program files stored on one (.D64) disk image. The 'programs' are actually 3 versions of the same program:

Name Max Program Size Max Variable Size
2-progs 27.6k (111 blocks) 31.3k
2g-progs 23.1k (93 blocks) 31.3k
3-progs 18.2k (73 blocks) 20.9k

The first character of the filename (a number) indicates the number of programs you can have in memory at once (2 or 3). Only the G version should be used with programs that use the VIC bitmap screen (this includes the SPRDEF editor).

 
  N-Progs  

There are only 2 steps needed to use N-Progs. First, load and start the version of your choice with:

BLOAD"N-PROGS",B0 (where N is 2, 2G, or 3)
BANK 0:SYS 65168 (or J FE90 in Monitor)

This will setup BASIC in 'memory space' zero (0). NOTE: This will erase any BASIC program in memory!

The second step is to switch memory spaces (change programs) at your leisure with:

BANK 0:SYS 65024,n (or J FE00 in Monitor with .A = n)

where n is the 'memory space' you want to switch to:

  • 0 or 1 (version 2 or 2g)
  • 0, 1, or 2 (version 3)
Note: after the switch, BASIC will be configured to BANK 15.

You can LOAD, edit, RUN, STOP, CONT, SAVE, a program in a memory space independant of programs in other memory spaces.

 
  How It Works  

Each program has its own area of Bank 0 for program storage. This includes the program along with any embedded DATA or DEFined functions. Using version 3 as an example:

Start Address End Address
Program 0 $1C00 $64FF
Program 1 $6500 $ADFF
Program 2 $AE00 $F6FF

Each program has its own area of Bank 1 for variables. This includes all variables, arrays, and strings.

Each program has its own BASIC stack which holds information about FOR/NEXT, DO/LOOP, and GOSUB/RETURN. This is stored in a 512-byte block near the top of Bank 0 when a program is inactive. Using version 3 as an example:

Start Address End Address
Stack 0 $F700 $F8FF
Stack 1 $F900 $FAFF
Stack 2 $FB00 $FCFF
The program of the current memory space has this information copied to the fixed location $800~$9FF in Bank 0.

Each program has pointers to memory limits, DATA, current line#, trap line#, error line#, error#, and PRINT USING characters (aka PUDEF) stored in the $FD00 block of memory when inactive. The program of the current memory space has this information copied into special BASIC locations in zero page and page 18 ($12xx).

Everything else is shared between all programs. This includes:

  • Open files (managed mainly by KERNAL)
  • Text / graphic colors
  • Cursor postion and Windows
  • Function key strings
  • Sprites (both images and movement)
  • Sound, Envelope, Volume, etc.
The reserved variables ST and TI are actually copied from the KERNAL so they are also shared.

BUT, the disk error status, DS$ is written to Bank 1 like normal variables so it is NOT shared (strictly speaking).

The program temporarily uses $50~53 when switching memory spaces. Data is stored from $F700 (version 3) or $F900 (version 2,2g) up to $FDFF. The ML program itself lies at $FE00~$FEFD. The current memory space number is stored at $FEFE.

 
  Bonus Features  

To be compatible with other utilities, the area $1300~$1BFF is NOT used by this program. Neither is the popular zero page memory $FA~$FF.

N-Progs will servive a reset. Although reset will erase program 0, you can recall other programs. NOTE: if you need to save your other programs, do so before running any programs in the default program 0 memory space! This is because the reset will cause BASIC to claim all memory for program 0. After saving (if neccessary), use the normal initialization call:

BANK 0:SYS 65168

This will reset all memory spaces correctly, but performs a NEW on each in the process.

 
  Known Problems  

During a TRAP, the CPU stack is saved and manipulated. This is different from the BASIC stack. TRAP can be used in programs, but your program must NOT switch to another program during the TRAP routine. The TRAP ends and the CPU stack is fixed when the RESUME command is executed. Program switching can occur again after RESUME.

SPRITES and the LightPen can cause collissions at any time. It is recommended to not use COLLISSION (not tested). BUMP should be okay.

Switching between running programs may fail depending on the types of 'nesting' in use on the stack. My limited tests show this:

Program 0 Structure Program 1 Structure Result
FOR/NEXT DO/LOOP OK
FOR/NEXT GOSUB/RETURN OK
DO/LOOP GOSUB/RETURN Crash!!!!
FOR/NEXT DO/FOR/GOSUB/RETURN/NEXT/LOOP OK
This is rather strange! I've had no problems with one running program switching to another memory space in immediate mode (and switching back to the running program tests okay too). To be safe, it is recommended you not switch spaces between two (or more) running programs.
© H2Obsession, 2007, 2014

Comments