Skip to content

Commit 84f03ce

Browse files
committed
Moved C source code to src/.
This means we can build our golang interpreter more easily.
1 parent a0f04e9 commit 84f03ce

File tree

8 files changed

+38
-16
lines changed

8 files changed

+38
-16
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ all: simple-vm embedded
2020
#
2121
# The sample driver.
2222
#
23-
simple-vm: main.o simple-vm.o simple-vm-opcodes.o
24-
$(LINKER) $@ $(OBJECTS) $(CFLAGS) main.o simple-vm.o simple-vm-opcodes.o
23+
simple-vm: src/main.o src/simple-vm.o src/simple-vm-opcodes.o
24+
$(LINKER) $@ $(OBJECTS) $(CFLAGS) src/main.o src/simple-vm.o src/simple-vm-opcodes.o
2525

2626

2727
#
2828
# A program that contains an embedded virtual machine and allows
2929
# that machine to call into the application via a custom opcode 0xCD.
3030
#
31-
embedded: embedded.o simple-vm.o simple-vm-opcodes.o
32-
$(LINKER) $@ $(OBJECTS) $(CFLAGS) simple-vm.o embedded.o simple-vm-opcodes.o
31+
embedded: src/embedded.o src/simple-vm.o src/simple-vm-opcodes.o
32+
$(LINKER) $@ $(OBJECTS) $(CFLAGS) src/simple-vm.o src/embedded.o src/simple-vm-opcodes.o
3333

3434

3535
#
3636
# Remove our compiled machine, and the sample programs.
3737
#
3838
clean:
39-
@rm simple-vm embedded *.raw *.o || true
39+
@rm simple-vm embedded *.raw src/*.o || true
4040

4141

4242

README.md

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,43 @@ simple.vm
77

88
This repository contains the implementation for a simple virtual-machine, along with a driver which will read a program from a file and execute it via that virtual machine.
99

10-
In addition to the virtual machine itself you'll also find:
10+
There are actually two intepreters, one written in C, and another written
11+
in golang.
1112

12-
* [A simple compiler](compiler).
13+
In addition to the virtual machine interpreter you'll also find:
14+
15+
* [A simple compiler](compiler), written in perl.
1316
* This will translate from assembly-source into binary-opcodes.
14-
* [A simple decompiler](decompiler).
17+
* [A simple decompiler](decompiler), written in perl.
1518
* This will translate in the other direction.
16-
* Several [example programs](examples/).
17-
* An example of [embedding](embedded.c) the virtual machine in a C host program.
19+
* Several [example programs](examples/) written in our custom assembly-language.
20+
* An example of [embedding](src/embedded.c) the virtual machine in a C host program.
1821
* Along with the definition of a custom-opcode handler.
19-
* A golang-interpreter for our bytecode.
20-
* Which is 100% complete and identical the C-based interpreter.
2122

2223
This particular virtual machine is intentionally simple, but despite that it is hopefully implemented in a readable fashion. ("Simplicity" here means that we support only a small number of instructions, and the registers the virtual CPU possesses can store strings and integers, but not floating-point values.)
2324
This particular virtual machine is register-based, having ten registers which can be used to store strings or integer values.
2425

2526

27+
Compilation
28+
-----------
29+
30+
Because the compiler and decompiler are written in Perl they need no special
31+
treatment.
32+
33+
There are two interpretters, one written in C and one in Golang. To build the
34+
C intepreter:
35+
36+
$ make
37+
38+
This will generate `simple-vm` and `embedded` from the contents of [src/](src/).
39+
40+
To build the golang interpreter:
41+
42+
$ go build .
43+
44+
This will generate `simple.vm` from the file [main.go](main.go/)
45+
46+
2647
Implementation Notes
2748
--------------------
2849

@@ -64,11 +85,11 @@ Embedding
6485

6586
This virtual machine is designed primarily as a learning experience, but it is built with the idea of embedding in mind.
6687

67-
The standard `simple-vm` binary, which will read opcodes from a file and interpret them, is less than 25k in size.
88+
The standard `simple-vm` binary, which will read opcodes from a file and interpret them, is less than 40k in size.
6889

6990
Because the processing of binary opcodes is handled via a dispatch-table it is trivially possible for you to add your own application-specific opcodes to the system which would allow you to execute tiny compiled, and efficient, programs which can call back into your application when they wish.
7091

71-
There is an example of defining a custom opcode in the file `embedded.c`. This example defines a custom opcode `0xCD`, and executes a small program which uses that opcode for demonstration purposes:
92+
There is an example of defining a custom opcode in the file `src/embedded.c`. This example defines a custom opcode `0xCD`, and executes a small program which uses that opcode for demonstration purposes:
7293

7394
$ ./embedded
7495
[stdout] Register R01 => 16962 [Hex:4242]
@@ -190,7 +211,8 @@ The result is `main.go` which executes all of the [included example programs](ex
190211
Sample usage:
191212

192213
$ ./compiler examples/jump.in
193-
$ go run main.go examples/jump.raw
214+
$ go build .
215+
$ ./simple.vm examples/jump.raw
194216
Loading file examples/jump.raw
195217
Steve Kemp
196218
32
@@ -199,7 +221,7 @@ Sample usage:
199221
Similarly you can run the looping example:
200222

201223
$ ./compiler examples/loop.in
202-
$ go run main.go examples/loop.raw
224+
$ ./simple.vm examples/loop.raw
203225
Loading file examples/loop.raw
204226
Counting from ten to zero
205227
10
File renamed without changes.

main.c renamed to src/main.c

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)