Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RISC-V CPU emulator made with Ruby

RISC-V CPU emulator made with Ruby

I talked at COSCUP x RubyConf Taiwan 2025

Avatar for hachi (Hayao Kimura)

hachi (Hayao Kimura)

August 09, 2025
Tweet

More Decks by hachi (Hayao Kimura)

Other Decks in Programming

Transcript

  1. 目次 • I’m Hachi • From Osaka Japan • Web

    application developer at freee • Co-founder of Kyobashi.rb • First time in Taiwan! Hayao Kimura (@hachiblog)
  2. 目次 • I’m Hachi • From Osaka Japan • Web

    application developer at freee • Co-founder of Kyobashi.rb • First time in Taiwan! Hayao Kimura (@hachiblog)
  3. 目次 • I’m Hachi • From Osaka Japan • Web

    application developer at freee • Co-founder of Kyobashi.rb • First time in Taiwan! Hayao Kimura (@hachiblog)
  4. 目次 • I’m Hachi • From Osaka Japan • Web

    application developer at freee • First time in Taiwan! Hayao Kimura (@hachiblog) 腸粉
  5. Key Takeaways from This Session • You can build a

    CPU emulator with ruby. • A CPU's basic implementation is surprisingly simple. • Ruby provides an accessible entry point for understanding hardware mechanisms, enabling easy CPU emulator creation and further exploration.
  6. Agenda • Why I build it with Ruby • What

    specifically I built • Result
  7. What if I made it a theme of learning basic

    information through practice?
  8. Why Ruby? • Ruby is the language I'm most familiar

    with. • Ruby(interpreted languages) CPU emulators: uncommon
  9. Registers CPU Decoder ALU Memory Program Counter Second step: Decode

    0x34202f73 4c771663 1001100011101110001011001100011
  10. Registers CPU Decoder ALU Memory Program Counter Second step: Decode

    0x34202f73 4c771663 1001100011101110001011001100011
  11. Registers CPU Decoder ALU Memory Program Counter Second step: Decode

    0x34202f73 4c771663 1001100011101110001011001100011 bne a4,t2,8000066c
  12. What is RISC-V • Open-source Instruction Set Architecture. • Adheres

    to the RISC (reduced instruction set computer) • Currently, fewer examples as PC CPUs; more common in microcontrollers and specific uses.
  13. What is RISC-V • Open-source Instruction Set Architecture. • Adheres

    to the RISC (reduced instruction set computer) • Currently, fewer examples as PC CPUs; more common in microcontrollers and specific uses.
  14. What is RISC-V • Open-source Instruction Set Architecture. • Adheres

    to the RISC (reduced instruction set computer) • Currently, fewer examples as PC CPUs; more common in microcontrollers and specific uses.
  15. Implemented Instructions ADD ADDI LUI AUIPC Arithmetic SW Store JAL

    Jump & Link SLLI Shift ORI Logical ECALL Environment CSRRS CSRRW CSRRWI CSR BEQ BNE BLT Branch
  16. Implementation Policy • Build basic Registers, Memory, Decoder. • Execute

    tests. • Raise exceptions for unimplemented instructions. ◦ Implement missing instructions. • Tests pass!
  17. Implementation Structure • Create RISCV32CPU class. • Use load_program method

    to load program into memory. • Use execute method to run the program.
  18. -1 + -1 = -2 󰢏 -1 + -1 =

    4294967294 󰢃
  19. What I learned and how it felt • Ruby's ease

    allows web engineers (or anyone interested in underlying mechanisms) to grasp CPU fundamentals. • It provides an accessible entry point to understanding hardware mechanisms. • This implementation omitted complexities; logic circuit construction was largely abstracted by Ruby's power. • For deeper CPU interest, consider emulating logic circuits or using Hardware Description Languages (HDLs). • Ruby provides an accessible entry point, sparking further interest in computer mechanisms.