The power of emulation:  Retro Programming

The code generator works similar to an interpreter, or one can generalise it more by saying that it is an "emulator".  We will not discussing emulation but I want to show you a kind of interpreter in work. I have a compiler (same as Rz compiler ) that I want to add an ability to display some graphics.  A usual way is to write it using Windows development tools such as Microsoft Visual Studio Community version (free).  It is good and reliable.  However, you have to know that a basic installation is 12Gbytes. (versus the demo here including everything is about 10Mbytes, a difference of 1000x).  If I choose to do that, I have to port my compiler (which is written in C) to the C++ language (another way is possible such as write in Win32 directly but ...).

I recall that I knew how to do this graphics things long time ago in bit-mapped graphics on old 16-bit machines.  So, I decide to do that and pick Turbo C 3.0 (free) which include ancient kind (simple) of graphics API.  The problem is, this kind of old software does not run on a contemporary system (such as Windows 10) any more.  A solution is to use an "emulator".  As I wrote most of my program for teaching using only standard input/output for a console, DOSBox is suitable.  That means DOSBox is a virtual machine that I can install and run old programs (so called DOS based).  So, I install and run Turbo C 3.0 inside DOSBox.  I port my Rz source to this platform.  I write a graphical extension for this Rz special.  Here is what my target program looks like.  It contains interrupts and "sleep and wait for interrupt".  This program is compiled into S2.1 machine code and runs under S2.1 simulator which is extended to be able to output to a graphic screen. The main loop just endlessly wait for interrupt ("doze").  One interrupt routine reads the system clock and display it ("int0").  The second interrupt reads a wave and plot it on the graphic screen.  This is a kind of two concurrent processes (but that is not the point of this demonstration). I call this style "low power programming" which I want to mean programming aimed to save energy.

Program

xaxis       // global

int0()
  x = readport(13)    // clock
  writeport(14,x,0)   // write to textbox

int1()
  y = readport(10)       // sin wave
  writeport(15,xaxis,100-y)  // plot graphic
  xaxis = xaxis + 2

main()
  xaxis = 10
  while( 1 )
    doze()    // sleep and wait for int

Here is the screen shot

screen shot

Discussion

This demo is actually a virtual CPU (sim.exe, simulator of S2.1) running on top of another emulator (DOSBox, emulate 8086 processor) which is running on a contemporary operating system (Windows).  You can see that "virtualization" is another strong point of a general purpose computer.  Given that the real CPU has enough power, we can almost certain that we can "emulate" all other computers, operating system, applications. This example shows all systems that run on similar instruction set x86.  However, the same principle will work with the emulation of different instruction set, different operating system, such as running an Android app which is built for ARM processor (mobile phone) on an Intel (Core-i, x64) processor on Windows. 

What you need to try out this demo

1) Turbo C 3.0  although we need only runtime, it requires the runtime graphic in the director \tc\bgi
2)  DOSBox
3)  My Rz compiler with the S2.1 simulator version plus graphic extension (executable version).  The executable "sim.exe" (simulator) and "twopro-s.obj" (S2.1 machine code) is located in  \s21tc\sim21 directory.  The whole source is in the package iot-rz-graphic.zip  (see link below).

How to run it

1) Run DOSBox, mount the virtual disk properly.  You need to be able to find \tc\bgi  and your executable "sim.exe" and "twopro-s.obj".   You may read DOSBox documentation.
2) Run my program in the DOSBox console   "d:sim twopro-s.obj"  and you will see a graphic screen pop up and live program run.

Tools

You can find my package containing Rz compiler, S2.1 simulator special edition here.  To learn more about this compiler system, have a look at this link that I recently teach a compiler class.  And this lecture shows you how to use this Rz special version compiler.

iot-rz-graphic.zip

How to run iot-rz with graphics in DosBox 0.74

Install DosBox 0.74.  Start DosBox

z:>

mount the directory to a disk

z:>mount d c:\temp\iot-rz
z:>d:


Change to the target directory.

d:>cd s21tc>sim21
d:>\s21tc\sim21>


Now we arrive at the destination directory.  The graphic configuration file from Turbo C must be presented here, EGAVGA.BGI (from Turbo C 3.0 directory BGI).

Run the simulator (sim.exe, compiled with Turbo C 3.0) with the input object file, twopro-s.obj produced from the assembler of S21 (asm.exe).

d:\sim21tc\sim21>sim twopro-s.obj

Done, Enjoy! 

Prabhas Chongstitvatana

last update 29 Aug 2017