# ----------------------------------------------------------------- # SSMC Simulator # (C) 2024 Andreas Bærentzen # This script is a simulator for the Super Simple Model Computer (SSMC). # For more information on the SSMC, see the following link: # https://people.compute.dtu.dk/janba/what_is_a_computer.html # ----------------------------------------------------------------- help_str = """ This script is a simulator for the Super Simple Model Computer (SSMC). Usage: > python ssmc_run.py > python ssmc_run.py > python ssmc_run.py -h|--help If you run this script with no arguments, an example SSMC program is used. If there is just one argument, the assembly code for the SSMC is read from the file provided as argument, unless the argument is '-h' or '--help', in which case this message is printed. The program should be provided as "assembly code" with the same format as the string below. memory_size: 6 0: x = memory[5] 1: x = x + 1 2: if x < 10: goto(1) 3: memory[5] = x 4: halt 5: -1 The first line specifies the size of the memory. The following lines specify what is in memory. Each line in the file contains a cell number followed by a colon and then the instruction or value stored in the cell. The instruction can be an arithmetic operation, a conditional goto, or a halt. Note that arithmetic operations are only allowed between short term memory values (or short term and a constant), and the results are written to short term memory. For more information about the instructions understood by the SSMC, see the following link: https://people.compute.dtu.dk/janba/what_is_a_computer.html It is important to point out that since there is no hardware specification of the SSMC, this script could never be a hardware simulator. In fact, it simply runs each line of the SSMC assembly code as a Python instruction. This means that you can write any valid Python code in the instructions. So, if you feel like it, you can go crazy and write a program that far exceeds the capabilities of the SSMC. However, if you stick to the simple instructions discussed in the document, this script is in effect an instruction set simulator for the SSMC. """ # ----------------------------------------------------------------- from os import system from sys import argv from time import sleep delay = 0.5 # Time in seconds to wait between instructions assembly_code = """ memory_size: 6 0: x = memory[5] 1: x = x + 1 2: if x < 10: goto(1) 3: memory[5] = x 4: halt 5: -1 """ # Function that checks if a string is an integer (also negative) def is_integer(s): return s.isdigit() or (s[0] == '-' and s[1:].isdigit()) # Function that checks if a string is a float def is_float(s): try: float(s) return True except ValueError: return False # Check if the user provided an SSMC assembly code file if len(argv) > 2 or len(argv)==2 and argv[1].lower() in ['-h', '--help']: print(help_str) exit(1) elif len(argv) == 2: try: assembly_code = open(argv[1]).read() except: print("Could not open the assembly file.") exit(1) # Parse the assembly code and put it into memory memory = [] for line in assembly_code.splitlines(): if ':' in line: pre_fix, post_fix = [ s.strip().lower() for s in line.split(':', 1) ] if pre_fix == 'memory_size': memory = [0]*int(post_fix) else: cell = int(pre_fix) if is_integer(post_fix): memory[cell] = int(post_fix) elif is_float(post_fix): memory[cell] = float(post_fix) else: memory[cell] = post_fix # Clear the short term memory (i.e. registers) and set the instruction # pointer to 0 a = 0 b = 0 c = 0 i = 0 j = 0 k = 0 x = 0 y = 0 z = 0 instruction = 0 # Define the SSMC goto instruction which is simply a Python function # that sets the instruction to the cell provided def goto(cell): global instruction instruction = cell-1 # Print the computer state. This includes the contents of the short # term memory and the contents of the memory. def print_computer(): out = '╔═══ Processor ══════════════╗\n' out += ' Instruction: %d' % instruction + '\n' out += ' Short Term Memory\n' out += f' a: {a:5} b: {b:5} c: {c:5}\n' out += f' i: {i:5} j: {j:5} k: {k:5}\n' out += f' x: {x:5} y: {y:5} z: {z:5}\n' out += '╚════════════════════════════╝\n' out += '╔═══ Memory ═════════════════╗\n' for cell in range(len(memory)): marker = "→ " if cell==instruction else " " out += marker + f"{cell} : {memory[cell]}\n" out += '╚════════════════════════════╝\n' print(out) # Main loop of the SSMC interpreter. The interpreter reads the instruction # from memory, executes it, and then increments the instruction pointer. system('clear') print_computer() sleep(delay) while memory[instruction] != 'halt': exec(memory[instruction], globals()) instruction = instruction + 1 system('clear') print_computer() sleep(delay)