(***************** Simple Instruction Set Computer *********************)
(** by Wen-Nung Tsai, tsaiwn@csie.nctu.edu.tw, written in Turbo Pascal**)
(** CopyLeft reserved:-)                        (:- CopyLeft reserved **)
(** COPY/Distribute it as long as you like it and provide             **)
(** all CopyLeft notices are maintained on this software source code. **)
(* Replace "Break" by "goto ..." if you use Turbo Pascal 6.0 or below. *)
(* 2010-03-10 Version 3.38 ---   fix memory overide bug when running   *)
(* 2010-03-10 Version 3.37 ---  DD?? put random number into Register D *)
(* 2010-03-10 Version 3.37 ---  DD?? instruction to generate random#   *)
(* 2010-03-10 Version 3.36 ---  Enable SETPC Directive for start addr. *)
(* 2010-03-08 Version 3.35 ---  Enable ORG directive when loading MCode*)
(* 1996-12-20 Version 3.33 ---  fix some bugs that cause SISC to crash *)
(* 1996-12-20 Version 3.32 ---  Enable CTRL_C when GETi reading integer*)
(* 1996-12-19 Version 3.31 ---  myReadln so that reading Unix file OK  *)
(* 1996-11-24 Version 3.3  ---  command X to generate Internal eXample *)
(* 1996-11-23 Version 3.25 ---  horselight changed + Dos shell         *)
(* 1996-11-03 Version 3.2  ---  horselight at bottom                   *)
(* 1995-12-30 Version 3.1  ---  command F for changing log File        *)
(* 1995-12-19 @ Hsinchu, Taiwan, R.O.C.                                *)
(***  The Source Program can be found in  ~csie1bta/public/sisc.pas  ***)
(***********************************************************************)
(***********************************************************************
    This Simple Instruction Set Computer (SISC) has the Architecture 
of the computer described in the Appendix B of our text book with some
extensions:
    1. The Memory has 1024 bytes. But you can toggle it between 256/1024.
    2. The Program Counter has 10 bits and thus can access 1024 bytes
       of the memory. However, it will use only 8 bits if the memory
       size is toggled to 256-byte mode.
    3. There is an internal STACK of 200 words which used for
       CALL ??? and RETURN instructions.
    4. I/O instructions are supported so that you can really
       write a program that it's work can be visible:-)
    5. Integer comparisom instruction can be used to compare two
       integers in registers and the conditional jump instructions
       JLT/JEQ/JGE will jump to a location (up to 3ff in hex) if
       the it match the status that affected by last CMP instruction.
--------------------------------------------------------------------------
   This program emulates the simple computer that described in Appendix B
   of the text book that written  by J.Glenn Brookshear:
     "Introduction to Computer Science: An Overview, fourth edition."
   However, the instruction set has been expanded.
       The additional instructions are listed below:(refer to pp.455-456)
       ****** Note that "?" means don't care
          0???   NOP   (No OPeration)
          D0??   GETC : Get a Char into R0
          D1??   OUTC : Output a Char from R0
          D2??   GETi : Get an integer into R0
          D3??   OUTi : Output integer from R0
          D5XY   GETS : Get a String into Memory starts from location XY
          D6XY   OUTS : Output String from Memory location XY till a '$'
 the followin instructions can have address that up to 3ffh (1023 decimal):
    (Note that  those "zzz xxxx yyyy" below are  in binaryform.)
         EZXY : determined by right 2 bits in Z (see below)
          Ezz00 xxxx yyyy: LOAD F,zzxxxxyyyy
          Ezz01 xxxx yyyy:STORE F,zzxxxxyyyy
          Ezz10 xxxx yyyy: CALL zzxxxxyyyy
          E??11 ???? ????: RETurn
         FZXY   : determined by right 2 bits in Z
           zzzz xxxx yyyy are all in binary form:
           ??00 xxxx yyyy:  CMP RX to RY     (will affect status:LT/EQ/GT)
           zz01 xxxx yyyy:  JLT zzxxxxyyyy    (Jump if the status is LT)
           zz10 xxxx yyyy:  JEQ zzxxxxyyyy
           zz11 xxxx yyyy:  JGT zzxxxxyyyy
         Only CMP instruction can affect LT/EQ/GT status
***********************************************************************)
(***********************************************************************)
(** by Wen-Nung Tsai, tsaiwn@csie.nctu.edu.tw, written in Turbo Pascal**)
(** CopyLeft reserved:-)                        (:- CopyLeft reserved **)
(** COPY/Distribute it as long as you like it and provide             **)
(** all CopyLeft notices are maintained on this software source code. **)
(* 1996-11-24 Version 3.3  ---  command X to generate Internal eXample *)
(* 1996-11-23 Version 3.25 ---  horselight changed + Dos shell         *)
(* 1996-11-03 Version 3.2  ---  horselight at bottom *)
(* 1995-12-30 Version 3.1  ---  command F for changing log File        *)
(* 1995-12-19 @ Hsinchu, Taiwan, R.O.C. *)
(***********************************************************************)
(***********************************************************************)

****** SISC LOG at 14:00:57 on Monday, 9/17/2001 ******
Yes> H
 
Command can be either Upper case
               or lower case:
  Q   Quit this system
  H/I Help message/Instructions
  A   Assemble assembly program
  B   set Break pointer
  E   show/Enter memory data
  L/S Load/Save the Machine Code
  P   set Program counter
  R?  modify content of Register ?
  G   Go (RUN) start from PC
  T   Trace the program one step
  U   Unassemble (4 instructions)
  M   Memory size toggle
  =+- show/change RUNNING speed
  other cmd:  C K F
Only CMP affects LT/EQ/GT status
Yes> Q

Thank you for using SISC!

C:\SISC> cat -l samp1.mc
   1 ;samp1.mc
   2 ;this is a sample Machine Code for the SISC
   3 ; that will print out the code of myself
   4 ; in decimal form
   5 2 1,01  ; R1=1  (00)
   6 2200   ; R2=0   (02)
   7 233A   ; R3 = length of this program
   8 3209   ; store r2 into 9 (:AGAIN = 06)
   9 16 00  ; LOAD r6 from ?? (:08 :09 )
  10 20 4d  ; r0="M"
  11 d1 00  ; print "M"
  12 20 28  ; "("
  13 
  14 d100   ; print "("   (:10h)
  15 40 2,0   ; move r2 to r0  (:12h)
  16 d300   ; print r0 as integer
  17 20 29 ; ")"
  18 d100
  19 203d;"="
  20 d100
  21 4060; move r6 to r0
  22 
  23 d300 ; out content of mem[r2] (:20h)
  24 200d; CR 
  25 d100
  26 20 0A ;LF
  27 
  28 d100  ; print Line Feed   (:28h)
  29 40 30 ; move r3 to r0 (program length)
  30 5221  ; r2 := r2+1 (r1 contains 1)
  31 B232  ; jump to done if R2=R0=R3
  32 
  33 b006  ; goto :AGAIN=06  (this line :30)
  34 2007  ; bell (:done = 32)
  35 d100  ; beep the speaker
  36 c000  ; halt
  37 ffff  ; for man check only
C:\SISC> sisc

     (進入全螢幕模式, 按 h 看 help )

Your running LOG is in SISC.LOG     <<------ 此即執行後之 LOG file --> 

C:\SISC> type sisc.log

****** SISC LOG at 00:06:00 on Friday, 12/22/1995 ******
Yes> H
 
Command can be either Upper case
               or lower case:
  Q   Quit this system
  H/I Help message/Instructions
  A   Assemble assembly program
  B   set Break pointer
  E   show/Enter memory data
  L   Load the Machine Code
  P   set Program counter
  R?  modify content of Register ?
  G   Go (RUN) start from PC
  T   Trace the program one step
  U   Unassemble (4 instructions)
  M   Memory size toggle
  =+- show/change RUNNING speed
  other cmd:  C K F
Only CMP affects LT/EQ/GT status
Yes> I
 
In addition to instructions on
our text book page 455-456,
("?" means don't care) FADD=NOP
 D0??/D1?? Get/Out Char to/from R0
 D2??/D3?? Get/Out int to/from R0
EZXY (zzzz xxxx yyyy in binary):
 Ezz00 xxxx yyyy: LOAD F,zzxxxxyyyy
 Ezz01 xxxx yyyy:STORE F,zzxxxxyyyy
 Ezz10 xxxx yyyy: CALL zzxxxxyyyy
 E??11 ???? ????: RETurn
 FZXY  depends on right 2 bits of ZZ
  zzzz in binary:
  ??00 xxxx yyyy:  CMP RX to RY
  zz01 xxxx yyyy:  JLT zzxxxxyyyy
  zz10 xxxx yyyy:  JEQ zzxxxxyyyy
  zz11 xxxx yyyy:  JGT zzxxxxyyyy
Only CMP affects LT/EQ/GT status
Yes> L
Filename: samp1.MC
=== Loading file samp1.MC ===
;sample1.mc
;this is a sample Machine Co
; that will print out the co
; in decimal form
0000: 2 1,01  ; R1=1  (00)
0002: 2200   ; R2=0   (02)
0004: 233A   ; R3 = length of this
0006: 3209   ; store r2 into 9 (:A
0008: 16 00  ; LOAD r6 from ?? (:0
000A: 20 4d  ; r0="M"
000C: d1 00  ; print "M"
000E: 20 28  ; "("
      
0010: d100   ; print "("   (:10h)
0012: 40 2,0   ; move r2 to r0  (:
0014: d300   ; print r0 as integer
0016: 20 29 ; ")"
0018: d100
001A: 203d;"="
001C: d100
001E: 4060; move r6 to r0
      
0020: d300 ; out content of mem[r2
0022: 200d; CR 
0024: d100
0026: 20 0A ;LF
      
0028: d100  ; print Line Feed   (:
002A: 40 30 ; move r3 to r0 (progr
002C: 5221  ; r2 := r2+1 (r1 conta
002E: B232  ; jump to done if R2=R
      
0030: b006  ; goto :AGAIN=06  (thi
0032: 2007  ; bell (:done = 32)
0034: d100  ; beep the speaker
0036: c000  ; halt
0038: ffff  ; for man check only
Yes> U
000:  2101  !.  LDI   1,01
002:  2200  ".  LDI   2,00
004:  233A  #:  LDI   3,3A
006:  3209  2.  STORE 2,09
008:  1600  ..  LOAD  6,00
00A:  204D   M  LDI   0,4D
00C:  D100  Q.  PUTC    
00E:  2028   (  LDI   0,28
010:  D100  Q.  PUTC    
012:  4020  @   MOVE  2,0 
Yes> U                       ( 連續 U 會接下去 )
014:  D300  S.  PUTi    
016:  2029   )  LDI   0,29
018:  D100  Q.  PUTC    
01A:  203D   =  LDI   0,3D
01C:  D100  Q.  PUTC    
01E:  4060  @`  MOVE  6,0 
020:  D300  S.  PUTi    
022:  200D   .  LDI   0,0D
024:  D100  Q.  PUTC    
026:  200A   .  LDI   0,0A
Yes> U
028:  D100  Q.  PUTC    
02A:  4030  @0  MOVE  3,0 
02C:  5221  R!  ADD   2,2,1 
02E:  B232  22  JUMP  2,32
030:  B006  0.  JUMP  0,06
032:  2007   .  LDI   0,07
034:  D100  Q.  PUTC    
036:  C000  @.  Halt 
038:  FFFF  ..  JGT   3FF 
03A:  0000  ..  NOP  
Yes> P
Enter new Program Counter:1a
Yes> U
01A:  203D   =  LDI   0,3D
01C:  D100  Q.  PUTC    
01E:  4060  @`  MOVE  6,0 
020:  D300  S.  PUTi    
022:  200D   .  LDI   0,0D
024:  D100  Q.  PUTC    
026:  200A   .  LDI   0,0A
028:  D100  Q.  PUTC    
02A:  4030  @0  MOVE  3,0 
02C:  5221  R!  ADD   2,2,1 
Yes> P
Enter new Program Counter:0
Yes> G                      ( G 指令會從 current PC 開始執行 )
M( 0)= 33
M( 1)= 1
M( 2)= 34
M( 3)= 0
M( 4)= 35
M( 5)= 58
M( 6)= 50
M( 7)= 9
M( 8)= 22
M( 9)= 9
M( 10)= 32
M( 11)= 77
M( 12)= 209
M( 13)= 0
M( 14)= 32
M( 15)= 40
M( 16)= 209
M( 17)= 0
M( 18)= 64
M( 19)= 32
M( 20)= 211
M( 21)= 0
M( 22)= 32
M( 23)= 41
M( 24)= 209
M( 25)= 0
M( 26)= 32
M( 27)= 61
M( 28)= 209
M( 29)= 0
M( 30)= 64
M( 31)= 96
M( 32)= 211
M( 33)= 0
M( 34)= 32
M( 35)= 13
M( 36)= 209
M( 37)= 0
M( 38)= 32
M( 39)= 10
M( 40)= 209
M( 41)= 0
M( 42)= 64
M( 43)= 48
M( 44)= 82
M( 45)= 33
M( 46)= 178
M( 47)= 50
M( 48)= 176
M( 49)= 6
M( 50)= 32
M( 51)= 7
M( 52)= 209
M( 53)= 0
M( 54)= 192
M( 55)= 0
M( 56)= 255
M( 57)= 255
^GYes> Rx              (Rx 表示 Clear all registers)
all Registers cleared
Yes> R1
R1= 00: 08
Yes> R2
R2= 00: 08
Yes> R0
R0= 00: 89
Yes> E                (表示要 Examine/Enter Memory data)
Location(hex)?40
  MEM[040h] = dec 0  = 00h : 60
  MEM[041h] = dec 0  = 00h : 12
  MEM[042h] = dec 0  = 00h : 

Yes> P
Enter new Program Counter:40   (輸入40將PC 改為 40h)
Yes> U
040:  6012  `.  FADD  0,1,2 
042:  0000  ..  NOP  
044:  0000  ..  NOP  
046:  0000  ..  NOP  
048:  0000  ..  NOP  
04A:  0000  ..  NOP  
04C:  0000  ..  NOP  
04E:  0000  ..  NOP  
050:  0000  ..  NOP  
052:  0000  ..  NOP  
Yes> T               (會從 40h 開始, 因PC 已被改為 40h)
Yes> R=              (R= 表示print all registers)
R0= 18 R1= 08 R2= 08 R3= 00    ( 注意我們做了 FADD R0, R1, R2 )
R4= 00 R5= 00 R6= 00 R7= 00    ( 也就是說 R0 := R1 + R2 )
R8= 00 R9= 00 RA= 00 RB= 00 
RC= 00 RD= 00 RE= 00 RF= 00 

Yes> Q

Thank you for using SISC!

C:\SISC> cat -l samp2.mc

   1 ;samp2.mc
   2 ;this machine program read N
   3 ; and calculate 1+2+...+N
   4 2101  ; R1=1
   5 d624  ; print string  "N="
   6 d200  ; read integer into R0
   7 29 01  ; R9 = 1
   8 2500  ; R5 = 0, used to store sum
   9 5 5 5,9  ; R5 = R5 +R9  :again
  10 B9 12  ; if done goto :ok
  11 5991  ; R9 = R9 + 1
  12 B0 0a  ; goto :again
  13 d6 28  ; print "Sum from 1 to "  :ok
  14 d300  ; out  int from R0
  15 d625  ; print "="
  16 4050  ; move r5 to r0 , the sum
  17 d300  ; out  int from R0
  18 d620  ; out cr/lf
  19 c000  ; halt
  20 0d0a  ; CR/LF  (:20h)
  21 2424  ; $
  22 4e3d  ; "N="   (:24h)
  23 2424  ; $$
  24 5375  ; Su     (:28h)
  25 6d20  ; m
  26 6672  ; fr
  27 6f6d  ; om
  28 2031  ;  1
  29 2074  ;  t
  30 6f20  ; o
  31 2424  ; $$
  32 c000
  33 ;end of this program

******** ******** ******** ******** ******** ********

注意 SISC.exe 只接受機器語言 (參考 samp.mc, samp2.mc, BATNUM.MC 等)
所以你必須把寫好的組語程式想辦法"翻譯"成機器碼,
並且寫成每列(line)一個指令 (用16進位四位數)
 (還是參考 samp.mc, samp2.mc, BATNUM.MC 等:)
如果偷懶想用 Assembler 翻譯,
可使用以前你們學長用 Perl 寫的 SISC 組譯器
 在目錄 asm 之下有 個子目錄 perl_SISC_assembler/ 
裡面有個文字檔  siscasm.txt
那個就是啦, 可再 UNIX 直接 perl siscasm.txt  in_file out_file
若要在 Windows執行, 你必須先去抓 PERL for windows
安裝好, 設好 Path, 阿也是打 perl siscasm.txt  in_file out_file
注意 in_file 是你的 SISC 組語程式, out_file 是生出的機器碼
不要呆呆的就打 in_file 和 out_file
還有, 因該 Assembler 不是很聰明,
若該列是 Label 如 here:  則後面不可再有任何資料!
阿就是 Label 必須獨立一列啦!
還有 Label + / - 一個值也不認識,
你要用別人的 Assembler 阿就忍著用吧!
照規矩用總比用手動翻譯較不易出錯吧!!
不然你可以把它改聰明一點 :-)
======