• Stars
    star
    161
  • Rank 233,470 (Top 5 %)
  • Language
    Python
  • License
    The Unlicense
  • Created over 11 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Educational open-source CPU suite (with assembler, linker and simulator)

Luz


1   Introduction

This document will help you get started using / hacking on Luz. It doesn't explain Luz in any depth - read the user manual for that.

1.1   What is Luz?

Luz is an open-source CPU suite. The suite includes:

  • A fully functional simulator of the Luz 32-bit RISC CPU.
  • An assembler and a linker for code written in the Luz assembly (LASM) language.

In the future, Luz will also feature a complete synthesizable VHDL implementation of the CPU. For the time being, a partial implementation is available in the experimental directory of the Luz source code tree.

The following diagram describes Luz:

https://raw.github.com/eliben/luz-cpu/master/doc/luz_proj_toplevel.png

1.2   License

The code of Luz is in the public domain. For more information, see the COPYING file in the main directory.

1.3   What is Luz useful for?

Note: this answer is from the time I originally worked on Luz, circa 2008-2010.

I don't know yet. It's a self-educational project of mine, and I learned a lot by working on it. I suppose that Luz's main value is as an educational tool. Its implementation focuses on simplicity and modularity, and is done in Python, which is a portable and very readable high-level language.

Luz can serve as a sample of implementing a complete assembler, a complete linker, a complete CPU simulator. Other such tools exist, but usually not in the clean and self-contained form offered by Luz. In any case, if you've found Luz useful, I'd love to receive feedback.

2   Getting started with Luz

2.1   Documentation

Start with this README and then take a look at the user manual for more details.

2.2   Dependencies

Luz is implemented in pure Python. To run it, you need to have a couple of things installed:

  • Python version 2.7 or 3.2+
  • The PLY Python library. Luz should work with all recent versions of PLY; I last tested it with 3.9

2.3   Structure of the source tree

  • doc: Documentation
  • luz_asm_sim: The Luz assembler/linker and simulator:
  • luz_asm_sim/lib: The implementation of Luz
  • luz_asm_sim/tests_full: Full tests of Luz that also serve as examples
  • luz_asm_sim/tests_unit: Unit tests for Luz
  • experimental: Experimental code and features (not currently suitable for use)

2.4   Running examples

Luz has a framework of "full tests" in luz_asm_lib/tests_full. Each full test is a directory with one or more .lasm files that implement a program in Luz assembly language (LASM). The full tests serve two roles:

  • As examples of using Luz
  • For testing the Luz suite on complete programs

See tests_full/README.rst for more details on how to run the full tests. Here I want to focus on one simple example.

2.4.1   The simple loop example

Open tests_full/loop_simple. In there you'll find a LASM file named loop.asm. Open it and take a look. This is a program that computes the sum of an array. The array is defined statically in the .data segment, and the program itself starts with the asm_main symbol (as all LASM programs should start - see the user's manual for more details). It iterates over the array in a loop and sums it into the $r8 register.

Take a few minutes to study the code: if you've seen any other assembly code before, it should be simple to understand. Consult the user's manual for the meaning of individual instructions.

Now, to run the example, make sure PYTHONPATH is set properly and execute:

python run_test_interactive.py -i loop_simple

You should see something like:

LUZ simulator started at 0x00100000

[0x00100000] [lui $sp, 0x13] >>

This is the interactive shell of the Luz simulator that allows you to execute the code step-by step, viewing the contents of registers, and so on. If this sounds like a debugger, you're right - it is. Type help to see the available commands and what they mean. Note that run_test_interactive.py can also run the whole test from start to finish, without the interactive prompt. Run it with -h to see the options.

Execute the following commands:

[0x00100000] [lui $sp, 0x13] >> s 100
[0x00100038] [halt] >> set alias 0
[0x00100038] [halt] >> r
$r0   = 0x00000000     $r1   = 0x00000000     $r2   = 0x00000000     $r3   = 0x00000000
$r4   = 0x00000000     $r5   = 0x00100050     $r6   = 0x00100050     $r7   = 0x00000000
$r8   = 0x0000021F     $r9   = 0x0010003C     $r10  = 0x00000000     $r11  = 0x00000000
$r12  = 0x00000000     $r13  = 0x00000000     $r14  = 0x00000000     $r15  = 0x00000000
$r16  = 0x00000000     $r17  = 0x00000000     $r18  = 0x00000000     $r19  = 0x00000000
$r20  = 0x00000000     $r21  = 0x00000000     $r22  = 0x00000000     $r23  = 0x00000000
$r24  = 0x00000000     $r25  = 0x00000000     $r26  = 0x00000000     $r27  = 0x00000000
$r28  = 0x00000000     $r29  = 0x0013FFFC     $r30  = 0x00000000     $r31  = 0x0010000C
  • The first command asks the simulator to step through 100 instructions
  • The second command disables displaying register alias names since the LASM code of loop_simple doesn't use them (see the user's manual about alias names)
  • The third command asks Luz to display the contents of all registers.

As you can see, the simulator is now at a halt instruction, which means the CPU stopped executing ("halted"). $r8 holds the sum of the array. Now you can enter the q command to quit the interactive simulator.

The other examples can be run similarly.

More Repositories

1

pycparser

🐍 Complete C99 parser in pure Python
Python
3,226
star
2

pyelftools

Parsing ELF and DWARF in Python
Python
2,013
star
3

code-for-blog

Code samples from my blog
Go
1,574
star
4

llvm-clang-samples

UNMAINTAINED: Examples of using the LLVM and Clang compilation libraries and tools
C++
1,187
star
5

raft

🚣 Raft implementation in Go
Go
1,045
star
6

pss

pss is a power-tool for searching inside source code files.
Python
327
star
7

pykaleidoscope

Implementation of the LLVM tutorial in Python
Python
292
star
8

static-server

A simple, zero-configuration HTTP server CLI for serving static files
Go
183
star
9

modlib

Go project layout with exported packages and command-line tools, using modules
Go
180
star
10

deep-learning-samples

Sample code for deep learning & neural networks
Python
170
star
11

bobscheme

An implementation of Scheme in Python and C++
C++
167
star
12

js-8080-sim

Intel 8080 assembler and simulator in JS, optimized for simplicity.
JavaScript
155
star
13

wasm-wat-samples

Samples of WebAssembly Text programs
WebAssembly
128
star
14

gemini-cli

Access Gemini LLMs from the command-line
Go
100
star
15

libjit-samples

Code samples for using libjit
C
91
star
16

go-sudoku

Toolkit for solving and generating Sudoku puzzles in Go
Go
88
star
17

wcx64

Clone of wc in x64 assembly
Assembly
87
star
18

c-unleashed-book-souce-code

Source code for the "C Unleashed" book by Richard Heathfield, Lawrence Kirby, et al.
C
70
star
19

gosax

gosax is a basic wrapper for stream parsing of XML (SAX) Go
Go
62
star
20

asdl_parser

Standalone ASDL parser for upstream CPython 3.x
Python
61
star
21

go-ungrammar

Ungrammar implementation and API in Go
Go
43
star
22

paip-in-clojure

🌅 Code from "Paradigms of Artificial Intelligence Programming" in Clojure
Clojure
39
star
23

cs344

Introduction to Parallel Programming class code
C++
31
star
24

go-sentencepiece

Go implementation of the SentencePiece tokenizer
Go
20
star
25

go-websocket-sample

Sample Go websocket server and JS client with tracing and JSON data interchange
Go
20
star
26

esms

Legacy - ESMS (Electronic Soccer Management Simulator)
C++
18
star
27

xmlgen

C
15
star
28

go-quines

Some quines in the Go programming language
Go
15
star
29

python3-samples

Samples of Python 3 code, libraries, etc.
Python
12
star
30

gogl

Go Generic Library
Go
11
star
31

tupperformula

JS demo of Tupper's formula
JavaScript
9
star
32

arm_asm_hacks

ARM assembly hacks
Assembly
8
star
33

lsystem

JavaScript
8
star
34

hackutils

Various hacky utils for Go
Go
8
star
35

gocloud-samples

Samples/hacks with gocloud
Go
7
star
36

jscool2asm

JavaScript
5
star
37

sample-go-cli

Sample Go CLI project structure
Go
4
star
38

colorful-lines

My JS clone of the Colorful Lines game
JavaScript
4
star
39

cl-in-clj

Common Lisp --> Clojure snippets/idioms translation
Clojure
3
star
40

gocdkx

Fork of google/go-cloud for experiments.
Go
3
star
41

so-tag-sentiment-analysis

Go
2
star
42

ollama-import-experiment

Go
2
star
43

line-plotting

JavaScript
2
star
44

go-travis-test

sandbox
Shell
2
star
45

cljmin

Minimal project template for Clojure
Clojure
2
star
46

diffuse-sim

JavaScript
1
star
47

sample-go-project-vscode

Go
1
star
48

onlisp-in-clojure

Clojure
1
star
49

4clojure-solutions

My solutions to https://www.4clojure.com/problems
Clojure
1
star