This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ardera's profile picture

Arderas DCPU-16 Emulator & Assembler v0.1

Started by ardera, 24 April 2018 - 02:57 PM
ardera #1
Posted 24 April 2018 - 04:57 PM
Arderas DCPU-16 Emulator and Assembler

What is a DCPU-16?
The DCPU-16 is a fictional CPU (architecture) that was originally designed for 0x10c, a game that Minecraft-Creator Notch worked on in 2012; which sadly is now abandoned. The game is set in the 1980s so the CPU is pretty primitive; containing a minimal instruction set and no things like protected memory areas. It does feature interrupts though, so it's at least a bit more advanced than the Apple 2.
You can find the Specification for the CPU here: https://gist.githubu...DCPU-16Spec.txt
Additionally, here are some Specs for the Hardware Devices (Clock, Keyboard, LEM1802/Monitor etc): https://github.com/l...-specifications

What can the program do?
It can compile DCPU-16 Assembly and emulate a DCPU-16 virtual machine (i.e. interpret machine code). Currently, the VM supports the following features
  • the complete v1.7 spec (so the one linked in the introduction); all opcodes and argument types
  • (Hardware-) Interrupts
  • LEM1802 (which is a 32x12 characters Monitor), Clock, Keyboard, M35FD (floppy disk drive) emulation
  • Memory Read and Write Callbacks -> possibility for code / data breakpoints
The Compiler uses LuLPEG (a full Lua version of LPEG), which is a Regex Library designed specifically to parse Grammars; so it's well suited for programming languages. It currently features:
  • all spec v1.7 opcodes & argument types
  • Error Messages and Positions
  • SOME preprocessor instructions (#OFFSET, #DEFINE, #INCLUDE, #NAMESPACE)
  • labels, referencing labels from other namespaces ("atlasos_kernel.VRAM_address")
  • generation and inclusion of assembly & symbol files
Where can I get it?
minified (recommended) version (87kb): pastebin get eXMiwb40 dcpu
unminified version (107kb): pastebin get 4tNdHrxK dcpu

Usage

dcpu compile [options]
  options:
	+source <path>
		Compiles and includes .asm or .symfile into outputfile
	-out <path>
		Outputs the compilation results to a file at <path> (Default is
		the a .dbin file with the filename of the first source file)
		filetype depends on the file ending.
	-compileIncludes
		The files included in  the source files will be compiled and included
		in the outputfile
	-bendian
		outputfile will be in big endian.
	<path>
		same as +source
dcpu exec/execute/run [options]
  options:
	<path> [@ <address>]
		Loads file into address <address> in RAM (Default address is 0)
	-silent
		Don't add a Monitor Device to the VM (no graphics output)
	-ops
		Print the average operations per second at the end of the emulation
	-maxt <time>
		Aborts the emulation after <time> seconds have passed
	-nopull
		Don't pull ANY Events using os.pullEvent. Faster, but will result
		in a TLWY error when run too long.
	-pinterval <time>
		Sets the interval at which events are pulled to <time> seconds
	-lendian
		next files will be loaded in little endian mode (Default)
	-bendian
		same as -lendian but uses big endian mode.
dcpu help/-help/--help/-h/--h
  Shows this usage.

Preprocessor Instructions
  • #DEFINE identifier value sets the value of symbol identifier to value
  • #OFFSET value value can be any hex/dec number or defined symbol; Sets the current address to value
  • #NAMESPACE name sets the current namespace for symbol definition and resolution to name
  • #INCLUDE "path/to/file.dasm_or_file.dsym" includes the symbols in the file to be used in the file currently being processed. Unless -compileIncludes was used, this does NOT include the source of the file.
Symbols/Identifiers have the following structure: namespace.symbolname
In your assembly code, you can also only write the symbolname. The Parser will then automatically rewrite the identifier to current_namespace.symbolname; where current_namespace is the last namespace set using the #namespace instruction or the title of the file. (the path of the file without the directory and file ending.) (Labels are symbols too, btw)
This means that, when coding in one single file, you can completely pretend the namespace system doesn't exist at all and just write the symbol names.

Screenshots
Spoiler




Examples
You can find some example assembly programs here: https://github.com/d...master/programs
AtlasOS runs too, though you can't compile it using this tool yet. You have to use the compiler given in the repository: https://github.com/0x10cAtlas/AtlasOS
(AtlasOS has a bug where the keyboard buffer is being cleared to early; resulting in some keys not being registered when fast typing. In the unminified Version you can set TDevKeyboard.do_clear to false to fix this)
I also tried running Admiral but the developer seems to not care a bit about being spec-conformant; He even changed the Hardware IDs in his code to something else to make it extra hard to get it run on a normal DCPU; god knows why. That's a bit sad IMO.

Performance
The performance is not too bad considering the fact that ComputerCraft is so slow: The program in the second example picture runs at about 50 000 operations per second.
The same program with -silent and -nopull commandline args runs at ~57 000 operations per second.

Upcoming Features / Not Yet Working
  • Compiler Macros
  • More preprocessor Instructions (ideally the full ANSI-C set)
  • commandline arguments for M35FD (= official Floppy Drive) &amp; Floppy Disk emulation
  • HMD2043 and HMU1440 Emulation (= inofficial Floppy Drive + inofficial Floppy format; spec: https://gist.github....ielKeep/2495578)
  • Custom Palettes &amp; Fonts for the LEM1802 display (Needed for some advanced programs, currently setting a custom palette / font will silently fail, that's why some programs' graphics look broken)
  • Using term.blit for rendering
  • Evaluation of constant arithmetic expressions (so something like SET I, 24*1*334 +1)
Known Bugs
  • Compiler doesn't parse statements when they are terminated by semicolons. (SET I, 3; doesn't work but SET I, 3 ; works)
Edited on 24 April 2018 - 05:23 PM
Sewbacca #2
Posted 26 April 2018 - 05:17 PM
That looks pretty awesome. I didn't tested it yet, but from what I've seen, this is something special. Well done.