Skip to content

Commit 01b14cd

Browse files
committed
Initial commit
0 parents  commit 01b14cd

File tree

10 files changed

+548
-0
lines changed

10 files changed

+548
-0
lines changed

LICENSE

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
evm-arduino
2+
Copyright (C) 2014, 2022 Boro Sitnikovski
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
evm-arduino
2+
===========
3+
4+
A virtual machine for Arduino, allowing dynamic program updates.
5+
6+
Based on my [implementation of CHIP-8](https://github.com/bor0/chip-8), where the gaming related instructions are removed, and instead instructions for IO pin controlling are added.
7+
8+
The following instructions that are related to games (drawing, etc.) are removed:
9+
10+
- `00E0`
11+
- `DXYN`
12+
- `EX9E`
13+
- `EXA1`
14+
- `FX0A`
15+
- `FX18`
16+
- `FX29`
17+
18+
The following instructions that are related to IO control are added:
19+
20+
- `EXA1` - sets the pin mode X to the pin (value in first register)
21+
- `E0A2` - does `analogRead` to the first register and stores the result in the second register
22+
- `E0A3` - does `analogWrite` to the first register using the value from the second register
23+
- `E0A4` - sleep for second register's value milliseconds
24+
25+
Registers are changed from 8 bit to 16 bit.
26+
27+
In order to reload a program, use the `0000` instruction to halt the VM and restart.
28+
29+
Tested on ESP32 Arduino (LOLIN D32).
30+
31+
References:
32+
33+
- http://en.wikipedia.org/wiki/CHIP-8
34+
- https://github.com/bor0/chip-8
35+
36+
Boro Sitnikovski

bin/example.asm

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
6005 ; V[0] = 5 (LED_BUILTIN for ESP32)
2+
E2A1 ; pinMode(V[0], 2); (OUTPUT for ESP32)
3+
4+
6101 ; V[1] = 1
5+
E0A3 ; digitalWrite(5, 1);
6+
7+
62FA ; V[2] = 250
8+
72FA ; V[2] += 250
9+
72FA ; V[2] += 250
10+
72FA ; V[2] = 1000
11+
E0A4 ; delay(1000);
12+
13+
6100 ; V[1] = 0
14+
E0A3 ; digitalWrite(5, 0);
15+
16+
E0A4 ; delay(1000);
17+
18+
1200 ; jump to start

bin/example.bin

26 Bytes
Binary file not shown.

bin/example.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
void setup() {
2+
pinMode(LED_BUILTIN, OUTPUT);
3+
}
4+
5+
void loop() {
6+
digitalWrite(LED_BUILTIN, HIGH);
7+
delay(1000);
8+
digitalWrite(LED_BUILTIN, LOW);
9+
delay(1000);
10+
}

evm-arduino.ino

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
This file is part of evm-arduino.
3+
4+
evm-arduino is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
evm-arduino is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with evm-arduino. If not, see <http://www.gnu.org/licenses/>.
16+
17+
*/
18+
#define WIFI_SSID "SSID"
19+
#define WIFI_PASSWORD "PASSWORD"
20+
21+
#include <HTTPClient.h>
22+
#include <WiFi.h>
23+
24+
#include <stdio.h>
25+
26+
#include "include/vm.h"
27+
28+
void setup()
29+
{
30+
Serial.begin(115200);
31+
delay(10);
32+
33+
printf("Connecting to WiFi");
34+
35+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
36+
37+
while (WiFi.status() != WL_CONNECTED) {
38+
delay(500);
39+
printf(".");
40+
}
41+
42+
printf("\nConnected to WiFi\n");
43+
}
44+
45+
void loop()
46+
{
47+
HTTPClient client;
48+
struct vm VM;
49+
50+
printf("Requesting (extended) CHIP-8 binary\n");
51+
52+
// Sometimes downloading from GitHub causes the device to reboot. Best store on a local web server without https
53+
//client.begin("https://raw.githubusercontent.com/bor0/evm-arduino/main/bin/example.bin");
54+
client.begin("http://192.168.0.108:8080/example.bin");
55+
client.GET();
56+
57+
printf("Initializing VM\n");
58+
59+
vm_init(&VM, client.getString());
60+
61+
printf("Entering VM loop\n");
62+
63+
while (!VM.halt) {
64+
vm_cycle(&VM);
65+
delay(1);
66+
}
67+
68+
printf("VM halted\n");
69+
}

include/opcodes.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
This file is part of evm-arduino.
3+
4+
evm-arduino is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
evm-arduino is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with evm-arduino. If not, see <http://www.gnu.org/licenses/>.
16+
17+
*/
18+
#ifndef OPCODES_H
19+
#define OPCODES_H
20+
#include "vm.h"
21+
22+
void parse_opcode(struct vm *, uint16_t);
23+
#endif

include/vm.h

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
This file is part of evm-arduino.
3+
4+
evm-arduino is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
evm-arduino is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with evm-arduino. If not, see <http://www.gnu.org/licenses/>.
16+
17+
*/
18+
#ifndef VM_H
19+
#define VM_H
20+
#include <HTTPClient.h>
21+
#include <stdint.h>
22+
#include <stdio.h>
23+
24+
struct vm {
25+
struct {
26+
/* 16 16-bit data registers named from V0 to VF. The VF register doubles as a carry flag. */
27+
uint16_t v[16];
28+
29+
/* The address register is 16 bits wide and is used with several opcodes that involve memory operations. */
30+
uint16_t I;
31+
32+
/* Program counter. */
33+
uint16_t pc;
34+
35+
/* Stack pointer. */
36+
uint16_t sp;
37+
} registers;
38+
39+
/* CHIP-8's memory addresses range from 200h to FFFh, making for 3,584 bytes. */
40+
uint8_t memory[0x0FFF];
41+
42+
/* The original 1802 version allocated 48 bytes for up to 12 levels of nesting; modern implementations normally have at least 16 levels. */
43+
uint16_t stack[16];
44+
45+
/* Halt variable. */
46+
uint8_t halt;
47+
48+
/* Timers. */
49+
uint8_t delay_timer;
50+
};
51+
52+
void vm_init(struct vm *, String);
53+
uint16_t calc_opcode(struct vm *);
54+
void vm_cycle(struct vm *);
55+
#endif

0 commit comments

Comments
 (0)