August 16th, 2013

Second Reality Code Review: Part 2 (Engine)

I love you Hoi-En.
As mentioned in the previous page, Second Reality backbone is made of :

The goal of this page is to provide pointers for any programmer that would want to read the Engine and the Loader (The DIS is on the next page).

Part 1: Introduction
Part 2: Engine
Part 3: Demo Interrupt Server
Part 4: Dev Vs Prod
Part 5: Parts

Engine code

The code of the engine is 100% ASM but it is very well written and fairly well commented :

In terms of pseudo-code it can be summarized as follow :

    exemus  db 'STARTMUS.EXE',0
    exe0    db 'START.EXE',0
    exe23   db 'ENDSCRL.EXE',0

       cli                         ; Disable all interrupts
       mov     ah,4ah              ; Deallocate all memory
       call checkall               ; Check for 570,000 bytes of mem, 386 CPU and VGA
       call file_getexepath        
       call dis_setint             ; Install Demo Interrupt Server on Interrupt 0fch
       call file_initpacking       ; Check exe signature (no tempering) !
       call file_setint            ; Replace DOS routines (only OPENFILE, SEEK and READ) on Interrupt 021h
       call flushkbd               ; Flush the keyboard buffer
       call  checkcmdline          ; check/process commandline

       ;======== Here we go! ========
       call vmode_init             ; Init VGA (not necessarly Mode13h or ModeX), each PARTs had its own resolution

       mov si,OFFSET exe0
       call executehigh            ; loaded to high in memory. Used for loading music loaders and stuff.
       call  _zinit ; Start music
       call  restartmus

       mov   si,OFFSET exe1     ;Parameter for partexecute: Offset to exec name
       call  partexecute
       ; Execute all parts until exe23

       call fademusic
       ;======== And Done! (or fatal exit) ========

       mov cs:notextmode,0
       call vmode_deinit


The steps are pretty straight forward to read:

  1. Installs the DIS interrupt server at Interrupt 0fch.
  2. Replace DOS system calls at Interrupt 021h (more on that in the Dev Vs Prod page).
  3. Upload the music to the soundcard via EMS memory.
  4. Start the music.
  5. Execute each part of the demo.
  6. Done !

Details of the execute routines :

      call  openfile ; Open the DOS executable for this PART
      call  loadexe  ; loads the specified exe file to memory, does relocations and creates psp
      call  closefile
      call  runexe   ;runs the exe file loaded previously with loadexe.
                     ; returns after exe executed, and frees the memory
                     ; it uses.


Memory manager

There were many legends about Second Reality using an elaborated memory manager via the MMU but there is no trace of this in the engine. Memory management is actually delegated to DOS : The engine starts by deallocating all the RAM and then distribute it on demand. The only fancy trick is the ability to allocated RAM from the end of the heap: This is done using the return value of DOS malloc when too much RAM is requested.


Second Reality DIS



Fabien Sanglard @2013