~rabbits/orca-toy

ffb6e29257817923a54243b6961b9212fe45a349 — neauoire 2 years ago
Init
7 files changed, 184 insertions(+), 0 deletions(-)

A .clang-format
A .gitignore
A LICENSE
A README.md
A build.sh
A demo.orca
A toy.c
A  => .clang-format +19 -0
@@ 1,19 @@
AlignAfterOpenBracket: DontAlign
AlignEscapedNewlines: DontAlign
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: TopLevel
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: WebKit
IndentCaseLabels: false
TabWidth: 4
IndentWidth: 4
ContinuationIndentWidth: 4
UseTab: ForContinuationAndIndentation
ColumnLimit: 0
ReflowComments: false
SortIncludes: false
SpaceBeforeParens: false
\ No newline at end of file

A  => .gitignore +1 -0
@@ 1,1 @@
toy
\ No newline at end of file

A  => LICENSE +21 -0
@@ 1,21 @@
MIT License

Copyright (c) 2017 Hundredrabbits

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => README.md +79 -0
@@ 1,79 @@
# GYO

[Gyo](https://wiki.xxiivv.com/site/gyo.html) is an imaginary computer with 16 registers, 256 addressable memory addresses. The cpu understands 16 opcodes, the assembler syntax understands `=constants` and `:labels`. Programs are loaded directly into memory to create procedural self-modifiable programs.

<img src='https://wiki.xxiivv.com/media/generic/gyo.jpg' width='200'/>

## Instructions

```
0x0  BRK  Break
0x1  JMP  Jump to position in memory
0x2  JCA  Jump to position in memory, when carryflag
0x3  JZE  Jump to position in memory, when zeroflag
0x4  PEK  Read status
0x5  POK  Write status
0x6  GET  Store in register
0x7  PUT  Store in memory
0x8  ADD  Add value to register, set carryflag when overflow
0x9  SUB  Substract value from register, set carryflag when overflow
0xA  EQU  Compare register with value, set zeroflag when equal
0xB  LES  Compare register with value, set zeroflag when less than
0xC  AND  Bitwise AND operator
0xD  XOR  Bitwise XOR operator
0xE  ROL  Bitwise rotate left
0xF  ROR  Bitwise rotate right
```

## Addressing

- `#ef` the raw value.
- `$ef` the value at address #ef in memory.
- `[e]` the value of the 15th register.
- `{e}` the value at the address of the 15th register.

## IO

To begin IO mode, activate the traps flag with `POK #08`.

```
POK #08  ; Set traps flag ON
GET      ; Read character from stdin
PUT [0]  ; Write character to stdout
POK #00  ; Set traps flag OFF 
```

## Status Register

The status register is distributed as follows, it shares the same byte as the register selector. The **halt** flag is used by the BRK instruction is stops the cpu, the **zero** flag is used for conditional instructions(EQU/LES/JEQ/JNE), the **carry** flag is set when a SUB/ADD instruction carries over the 8bit range and the **traps** flag is set when the pointer should be sent to the traps location(in IO operations). 

```
T C Z H
| | | +---- Halt
| | +------ Zero
| +-------- Carry
+---------- Traps
```

## Assembly Syntax

```
=rate #01           ; defines constant value

:label              ; a label defines an address in the program
	ADD rate        ; add rate to register
	EQU #0f         ; set zeroflag if register is equal to #0f
	JZE end         ; goto end if equal
	JMP label       
:end
	BRK
```

If you need to fill the memory with specific values, you can do it as follows:

```
:hello
	#48 #65
	#6C #6C
	#6F #0A
```
\ No newline at end of file

A  => build.sh +13 -0
@@ 1,13 @@
#!/bin/bash

clang-format -i toy.c

rm -f ./toy

# GNU/Linux
cc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined -o toy toy.c

# Plan9
# pcc toy.c -o toy

./toy demo.orca

A  => demo.orca +6 -0
@@ 1,6 @@
...........
.S.........
...........
...........
.E.........
...........

A  => toy.c +45 -0
@@ 1,45 @@
#include <stdio.h>

typedef struct Grid {
	int width;
	int height;
	int length;
	char data[256 * 256];
} Grid;

int
error(char *name)
{
	printf("Error: %s\n", name);
	return 0;
}

void
run(FILE *f, Grid *g)
{
	char c;
	g->length = 0;
	while((c = fgetc(f)) != EOF) {
		if(c == '\n') {
			if(g->width == 0)
				g->width = g->length;
			g->height = g->length / g->width;
		}
		g->length++;
	}
	printf("grid:%d(%dx%d)\n", g->length, g->width, g->height);
}

int
main(int argc, char *argv[])
{
	FILE *f;
	Grid g;
	if(argc < 2)
		return error("No input.");
	f = fopen(argv[1], "r");
	if(f == NULL)
		return error("Missing input.");
	run(f, &g);
	return 0;
}