System / User Mode and Multi Tasking Flex.

Diego Trampus has suggested adding a Sytem / User bit to disable the DAT in when running
applications programs to free up more memory space and protect the operating system.
When an interrupt occurs the registers are optionally pushed on the stack and the interrupt vector
is fetched from the top of memory. The Bus status lines assert an interrupt acknowledge
cycle when the vector is read. The Bus status lines can be used to set the system mode flag,
which can in turn be used to load another DAT page for the interrupt service routines,
and to vector to the appropriate service routine.

An interrupt may be SWI, SWI2, SWI3, IRQ, NMI, FIRQ or RESET. All will cause the
board to enter the protected System or Supervisor mode so that the CPU can access IO
resources such a as the DAT. DAT table zero should be used for the system mode. DAT
pages 1 to 15 may be used for other user configurations.

FLEX is not re-entrant, but it is possible to multi task FLEX by running multiple copies
of the Aplications Interface and FMS and have the IO drivers trap into a multitasking
kernel. The kernel can then arbitrate reseources. There are other reasons why you might
want the IO service routines to be handled by a seperate process, and this has to do with
running the FLEX  file system under another file system.
 
A senario for multitasking FLEX is as follows:
When a user application is started, a copy of  FLEX and the user application itself  is loaded
into user memory and execution is started at the transfer address of the application.
When loading the user application, user memory must be paged into the system kernel
space in a 4Kbyte page, so a special paged loader is required.

The Logical to Physical address map for each user process must be kept in the system kernel,
so that the system knows which physical page to map in for IO operations in the kernel.
For instance, a FLEX application may need to read a file. The File Control Block for reading
the disk is stored in the applications data space. To read the sector a call must be made to the
IO kernel which will read the disk drive and store the data in the User RAM.

The FCB in user RAM must be mapped into kernel space.
Because the FCB can occur anywhere in memory, it may occur over a 4K page boundary
so 2 contiguous 4K pages from the user space must be mapped into kernel space. The FCB
address is stored in the X index register. This must be rounded down to a 4K page boundary
and the high order 4 bits are formed by the appropriate page in the kernel.
 
To find the page, we must know the User DAT Page number and the top 4 bits of the FCB address
(A12 - A15) Theses are used to index the DAT table to find the physical memory address.
This physical address is then mapped into the kernel space through DAT bank 0 and the kernel
FCB address is adjusted to kernel space. The sector is then read into the user FCB in Kernel space
and execution is returned to user space and the calling application.

To use multitasking properly you need to use the DAT bank select to load the appropriate
memory map for the USER application. The DAT bank number must be loaded when the
computer enters user mode, via a return from interrupt (RTI). Because there is no bus status
line for user mode, Diego Trampus suggests using a "Fuse" counter that delays the DAT
mapping for the appropriate number of clock cycles after the user DAT page register and
the user stack pointer is loaded and an RTI instruction is executed.

The application user memory will be mapped in during the RTI so that the saved registers
are re-loaded from user space memory rather than the kernel..

Typically a user application would have 64K bytes of RAM allocated, with the FLEX
applications interface and FMS loaded into user space. when any IO is requested, such as
reading a disk file into memory or outputting a character to a  the terminal, a Software interrupt
is executed to enter system mode.