{"id":17557537,"url":"https://github.com/blaknite/cpu-emulator","last_synced_at":"2025-09-09T04:43:50.167Z","repository":{"id":54572535,"uuid":"60887802","full_name":"blaknite/cpu-emulator","owner":"blaknite","description":"Emulating an 8-bit CPU in Ruby","archived":false,"fork":false,"pushed_at":"2023-12-28T02:02:04.000Z","size":82,"stargazers_count":13,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T07:06:00.165Z","etag":null,"topics":["8-bit","cpu","emulator","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blaknite.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-11T02:43:14.000Z","updated_at":"2023-12-28T02:02:08.000Z","dependencies_parsed_at":"2022-08-13T20:10:22.440Z","dependency_job_id":null,"html_url":"https://github.com/blaknite/cpu-emulator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blaknite%2Fcpu-emulator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blaknite%2Fcpu-emulator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blaknite%2Fcpu-emulator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blaknite%2Fcpu-emulator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blaknite","download_url":"https://codeload.github.com/blaknite/cpu-emulator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250566445,"owners_count":21451230,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["8-bit","cpu","emulator","ruby"],"created_at":"2024-10-21T09:08:21.178Z","updated_at":"2025-04-24T04:51:19.907Z","avatar_url":"https://github.com/blaknite.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Emulating an 8-bit Computer design in Ruby\n\n[![Build status](https://badge.buildkite.com/79601937713cccb07e1f81f06497cb7d06f5c6b3c11b1f5fb0.svg)](https://buildkite.com/blaknite/cpu-emulator)\n\nStarted out as a 4-bit computer but grew as I wanted to do more with it. Original idea here:\n\nhttps://www.thelanbox.com.au/forum/t/building-a-4-bit-computer-from-scratch\n\nThe CPU uses a minimal instruction set with provisions for conditional jumps and subroutines.\nEach op-code is 4 bits with instructions being either 8 or 16 bits in length.\n\n## Schematic\n\n![schematic diagram](https://d1bh5m8o3ysx9y.cloudfront.net/uploads/images/943c727f-70fc-4670-8026-ad860d14fe9d.png)\n\n## Instructions\n\n### Single-byte Instructions\n```\n+-------------------+\n| o o o o | d d d d |\n+-------------------+\n\n+-------------------+\n| op-code | ignored |   RET ignores data.\n+-------------------+\n\n+-------------------+\n| op-code | address |   Register operations use 4-bits for address.\n+-------------------+\n```\n\n### Double-byte Instructions\n```\n+-----------------------------------+\n| o o o o | d d d d d d d d d d d d |\n+-----------------------------------+\n\n+-----------------------------------+\n| op-code |      memory address     |   Jump and memory operations use 12-bits for address.\n+-----------------------------------+\n\n+-----------------------------------+\n| op-code | ignored |     data      |   Immediate operations use second byte for data.\n+-----------------------------------+\n```\n\n### Op-Codes\n```\nHEX : BIN  : CODE :  T : DESCRIPTION\n----+------+------+----+------------------------------------------------------------\n0x0 : 0000 : JMP  :  9 : Jump to memory address unconditionally.\n0x1 : 0001 : JC   :  9 : Jump to memory address if carry.\n0x2 : 0010 : JZ   :  9 : Jump to memory address if zero.\n0x3 : 0011 : CALL : 10 : Jump to memory address and save previous address to stack.\n0x4 : 0100 : RET  :  5 : Jump to memory address one down in stack.\n0x5 : 0101 : LD   :  7 : Load register to accumulator.\n0x6 : 0110 : LDI  : 11 : Load immediate value to accumulator.\n0x7 : 0111 : LDM  : 11 : Load memory to accumulator.\n0x8 : 1000 : ST   :  7 : Store accumulator in register.\n0x9 : 1001 : STM  : 11 : Store accumulator in memory.\n0xa : 1010 : NOR  : 10 : Logical NOR of register and accumulator.\n0xb : 1011 : NORI : 14 : Logical NOR of immediate value and accumulator.\n0xc : 1100 : ADD  : 10 : Add register to accumulator.\n0xd : 1101 : ADDI : 14 : Add immediate value to accumulator.\n0xe : 1110 : CMP  :  8 : Compare register with accumulator.\n0xf : 1111 : CMPI : 12 : Compare immediate value with accumulator.\n\n* T is the number of clock cycles required to perform the instruction\n```\n\n## Register-mapped Serial I/O\n\nRegisters `0xe` to `0xf` are reserved as Serial I/O registers and are attached to your terminal\nat run-time. Write to `0xf` to display something on the terminal and read from `0xe` to get the\nterminal input. I/O operates with a circular buffer of 8 bytes which will write over itself when\nfull.\n\n## Example\n\n```\n$ cat examples/test.asm\n; calculate 4 + 4\n\nSTART   LDI   0x4\n        ADDI  0x4\n        STM   0xfff\n\nDONE    JMP   DONE\n\n$ ruby bin/assemble.rb examples/test.asm examples/test.bin\nAssembling program..........complete!\n0x60 0x04 0xd0 0x04 0x9f 0xff 0x00 0x06\n\n$ ruby bin/run.rb examples/test.bin\nProgram loaded!\nRunning...\n\n$ cat tmp/debug.log\nProgram loaded!\nRunning...\n0x000 - |####|####|###| - LDI\n0x002 - |####|####|######| - ADDI\n0x004 - |####|####|###| - STM\n0x006 - |####|####|#| - JMP\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblaknite%2Fcpu-emulator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblaknite%2Fcpu-emulator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblaknite%2Fcpu-emulator/lists"}