-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_1.0 - Spring 2024 _Cycle_Accurate_Simulation
1 lines (1 loc) · 10.9 KB
/
Problem_1.0 - Spring 2024 _Cycle_Accurate_Simulation
1
{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"id":"9eaoxYqhYn3O"},"outputs":[],"source":["!pip install numpy"]},{"cell_type":"markdown","metadata":{"id":"s0t-IYobANFW"},"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6EdVSb2FY6le"},"outputs":[],"source":["#importing the numpy package\n","import numpy as np"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"lz6SWGeeZRuv"},"outputs":[],"source":["#1. SLI instruction implementation\n","def execute_SLI(rd, imm):\n"," # Shift the lower four bits of rd into the upper four bits, then load 4-bit imm into the lower four bits of rd\n"," registers[rd] = (registers[rd] << 4) | imm"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nLEFOW_RZejA"},"outputs":[],"source":["# 2.ADD instruction implementation\n","def execute_ADD(rd, rs):\n"," # Add registers rd and rs, store the result in rd\n"," registers[rd] += registers[rs]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"aRSmsJsrZwej"},"outputs":[],"source":["#2 SUB instruction implementation\n","def execute_SUB(rd, rs):\n"," # Subtract registers rs from rd, store the result in rd\n"," registers[rd] -= registers[rs]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OR0OXLRXaAv1"},"outputs":[],"source":["#3 HALT instruction implementation\n","def execute_HALT():\n"," # Stops the processor\n"," global halt\n"," halt = True"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"UffQp9ubaW0x"},"outputs":[],"source":["#4 STORE instruction implementation\n","def execute_STORE(rd, rs):\n"," # Store value in rd into memory at address rs\n"," memory[registers[rs]] = registers[rd]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CWTQQKOKagvp"},"outputs":[],"source":["#5 LOAD instruction implementation\n","def execute_LOAD(rd, rs):\n"," # Load value from memory at address rs into rd\n"," registers[rd] = memory[registers[rs]]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-QDQOjmIav1A"},"outputs":[],"source":["#6 SKIPZ instruction implementation\n","def execute_SKIPZ(rd):\n"," # Skip the next instruction if rd is zero\n"," global pc\n"," if registers[rd] == 0:\n"," pc += 1\n","\n","# SKIPNZ instruction implementation\n","def execute_SKIPNZ(rd):\n"," # Skip the next instruction if rd is not zero\n"," global pc\n"," if registers[rd] != 0:\n"," pc += 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"h_FIsidMa4UD"},"outputs":[],"source":["#7 JALR instruction implementation\n","def execute_JALR(rd, rs):\n"," # Save PC+1 into rd, jump to rs\n"," global pc\n"," registers[rd] = pc + 1\n"," pc = registers[rs]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"iXWBX_SHeK0p"},"outputs":[],"source":["# NAND instruction implementation\n","def execute_NAND(rd, rs):\n"," # Compute bitwise NAND of rd and rs, store the result in rd\n"," registers[rd] = ~(registers[rd] & registers[rs])\n","\n","# INC instruction implementation\n","def execute_INC(rd):\n"," # Increment value in rd\n"," registers[rd] += 1\n","\n","# DEC instruction implementation\n","def execute_DEC(rd):\n"," # Decrement value in rd\n"," registers[rd] -= 1\n","\n","# IN instruction implementation\n","def execute_IN(rd):\n"," # Wait for user input and store it in rd\n"," registers[rd] = int(input(\"Enter input: \"))\n","\n","# OUT instruction implementation\n","def execute_OUT(rd):\n"," # Print value in rd\n"," print(\"Output:\", registers[rd])\n","\n","# NOP instruction implementation\n","def execute_NOP():\n"," # Do nothing\n"," pass"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":501,"status":"ok","timestamp":1713465222367,"user":{"displayName":"Martin Sure Kondiwa","userId":"17309152213992783019"},"user_tz":-180},"id":"xJbxR3grhDmS","outputId":"5f78a337-0618-482c-ac40-69cfd2e55b12"},"outputs":[{"name":"stdout","output_type":"stream","text":["Registers: [0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n","Memory: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"," 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"," 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"," 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"," 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"," 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"," 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n"]}],"source":["# Define global variables\n","registers = np.zeros(16, dtype=np.int32)\n","memory = np.zeros(256, dtype=np.int32)\n","pc = 0\n","halt = False\n","\n","# Define instructions to execute\n","instructions = [\n"," {\"opcode\": \"SLI\", \"operands\": (1, 5)},\n"," {\"opcode\": \"ADD\", \"operands\": (2, 3)},\n"," {\"opcode\": \"SUB\", \"operands\": (4, 5)},\n"," {\"opcode\": \"HALT\", \"operands\": None},\n"," {\"opcode\": \"STORE\", \"operands\": (6, 7)},\n"," {\"opcode\": \"LOAD\", \"operands\": (8, 9)},\n"," {\"opcode\": \"SKIPZ\", \"operands\": (10,)},\n"," {\"opcode\": \"SKIPNZ\", \"operands\": (11,)},\n"," {\"opcode\": \"JALR\", \"operands\": (12, 13)},\n"," {\"opcode\": \"NAND\", \"operands\": (14, 15)},\n"," {\"opcode\": \"INC\", \"operands\": (16,)},\n"," {\"opcode\": \"DEC\", \"operands\": (17,)},\n"," {\"opcode\": \"IN\", \"operands\": (18,)},\n"," {\"opcode\": \"OUT\", \"operands\": (19,)},\n"," {\"opcode\": \"NOP\", \"operands\": None}\n","]\n","\n","# Function to execute an instruction\n","def execute_instruction(instruction):\n"," opcode = instruction[\"opcode\"]\n"," operands = instruction[\"operands\"]\n","\n"," if opcode == \"SLI\":\n"," execute_SLI(*operands)\n"," elif opcode == \"ADD\":\n"," execute_ADD(*operands)\n"," elif opcode == \"SUB\":\n"," execute_SUB(*operands)\n"," elif opcode == \"HALT\":\n"," execute_HALT()\n"," elif opcode == \"STORE\":\n"," execute_STORE(*operands)\n"," elif opcode == \"LOAD\":\n"," execute_LOAD(*operands)\n"," elif opcode == \"SKIPZ\":\n"," execute_SKIPZ(*operands)\n"," elif opcode == \"SKIPNZ\":\n"," execute_SKIPNZ(*operands)\n"," elif opcode == \"JALR\":\n"," execute_JALR(*operands)\n"," elif opcode == \"NAND\":\n"," execute_NAND(*operands)\n"," elif opcode == \"INC\":\n"," execute_INC(*operands)\n"," elif opcode == \"DEC\":\n"," execute_DEC(*operands)\n"," elif opcode == \"IN\":\n"," execute_IN(*operands)\n"," elif opcode == \"OUT\":\n"," execute_OUT(*operands)\n"," elif opcode == \"NOP\":\n"," execute_NOP()\n","\n","# Execute instructions one by one\n","for instruction in instructions:\n"," if not halt:\n"," execute_instruction(instruction)\n"," else:\n"," break\n","\n","# Print the contents of registers and memory\n","print(\"Registers:\", registers)\n","print(\"Memory:\", memory)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"x131H2cM5prl"},"outputs":[],"source":["\n","\n","# Function to print the diagram\n","def print_diagram():\n"," print(\" +-----+ +-----+ +-----+ +------+\" )\n"," print(\" | IN |----->| ADD |----->| SUB |----->| HALT |\")\n"," print(\" +-----+ +-----+ +-----+ +------+\" )\n"," print(\" | ^ | ^ | ^\")\n"," print(\" | | | | | |\")\n"," print(\" v | v | v |\")\n"," print(\" +-----+ +-----+ +-----+\")\n"," print(\" | NOP | | INC | | DEC |\")\n"," print(\" +-----+ +-----+ +-----+\")\n"," print(\" | | |\")\n"," print(\" v v v\")\n"," print(\" +-----+ +-----+ +-----+\")\n"," print(\" | JALR| | NAND| | OUT |\")\n"," print(\" +-----+ +-----+ +-----+\")\n"," print(\" | | |\")\n"," print(\" v v v\")\n"," print(\" +-----+ +-----+ +-----+\")\n"," print(\" | IN | | LOAD| | SKIPZ|\")\n"," print(\" +-----+ +-----+ +-----+\")\n"," print(\" | |\")\n"," print(\" v v\")\n"," print(\" +-----+ +-----+\")\n"," print(\" | STORE| | SKIPNZ|\")\n"," print(\" +-----+ +-----+\")"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"background_save":true,"base_uri":"https://localhost:8080/"},"id":"i4BKJM-X6zAY","outputId":"f282f66f-459b-48d4-da45-935498cac227"},"outputs":[{"name":"stdout","output_type":"stream","text":[" +-----+ +-----+ +-----+ +------+\n"," | IN |----->| ADD |----->| SUB |----->| HALT |\n"," +-----+ +-----+ +-----+ +------+\n"," | ^ | ^ | ^\n"," | | | | | |\n"," v | v | v |\n"," +-----+ +-----+ +-----+\n"," | NOP | | INC | | DEC |\n"," +-----+ +-----+ +-----+\n"," | | |\n"," v v v\n"," +-----+ +-----+ +-----+\n"," | JALR| | NAND| | OUT |\n"," +-----+ +-----+ +-----+\n"," | | |\n"," v v v\n"," +-----+ +-----+ +-----+\n"," | IN | | LOAD| | SKIPZ|\n"," +-----+ +-----+ +-----+\n"," | |\n"," v v\n"," +-----+ +-----+\n"," | STORE| | SKIPNZ|\n"," +-----+ +-----+\n"]}],"source":["# Print the diagram after executing each instruction\n","print_diagram()\n","\n","# Execute instructions one by one\n","for instruction in instructions:\n"," if not halt:\n"," execute_instruction(instruction)\n"," else:\n"," break"]}],"metadata":{"colab":{"provenance":[{"file_id":"10AjrWdvqakLFKAi3zY0zxPlRmy_VvKSh","timestamp":1713432340586}],"authorship_tag":"ABX9TyMDcuC6L81fKRtV3QEgjEl2"},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}