Technical specifications:
clock rate: 	384kHz
memory:		64k words
cache:		256 words instruction, 256 words data
		2-way associative, replacement policy: LRU
word length:	32-bit
registers:	32
fpu:		two 4-word stacks

some of these values can be accessed in software through these labels by including libLabel.txt:

label:				verbose:
CPU_CLOCKRATE			clock rate for the cpu
CPU_RAM				total amount of memory addresses in ram
CPU_MAXADDRESS			maximum addressable location in memory


Registers:

r0->r12 register 0 to 12
note:
	r0	can also be accessed as "zero" and always contains the value 0

a0->a12 	register 16 to 28. Can be used exactly as the other registers, they just use a different naming scheme.

Other registers:
	ra (Return Address)	is set to pc when call/callr is used and pc-1 when loop is used.
	sp (Stack Pointer)	is used by the push/pop instructions. Should always be set to point at a stack.
	fp (Frame Pointer)	used to store/restore the stack pointer.
	ex (EXtra)		contains overflow/underflow from addition, subtraction, multiplication and bitshifting. It also contains the remainder after division.
	es (Exception Status)	when an interrupt is triggered this register is set to the interrupt number
	ea (Exception Address)	when an interrupt is triggered this register is set to pc
	pc (Program Counter)	points to the next instruction. cannot be explicitly used in an instruction.


Instructions:

Components of instructions:

rA = first register specified
rB = second register specified
rC = third register specified
C  = constant, 16-bit unsigned integer
lC = long constant, 21-bit unsigned integer
pc = program counter, 16-bit unsigned integer (never explicitly specified)

Most instructions are written on the form
"instruction rA rB rC/C" (Eg. "add	r1, r2, r3" or "subi	r1, r2, 4")
rA is usually the destination register, and rB and rC/C are operands. Exceptions are in branching instructions (rA and rB are compared and rC/C is destination) and various other misc. instructions.
rA, rB and rC/C are named after the order they appear in the opcodes. Some instructions do not use all three registers, but they are always specified in that order (rA, rB, rC/C/lC).
the constant can be declared as a hexadecimal number (prefixed with 0x), a binary number(prefixed with 0b) or a base-10 number (no prefix)

Name:		Verbose:			Result:				Example:

add		ADD				rA = rB + rC			add	r1, r2, r3
addi		ADD Immediate			rA = rB + C			addi	r1, r2, 4
sub		SUBtract			rA = rB - rC			sub	r1, r2, r3
subi		SUBtract Immediate		rA = rB - C			subi	r1, r2, 4
mul		MULtiply			rA = rB * rC			mul	r1, r2, r3
muli		MULtiply Immediate		rA = rB * C			muli	r1, r2, 4
div		DIVide				rA = rB / rC			div	r1, r2, r3
divi		DIVide Immediate		rA = rB / C			divi	r1, r2, 4
mov		MOVe				rA = rB				mov	r1, r2
movi		MOVe Immediate			rA = C				movi	r1, 4
movil		MOVe Immediate Long		rA = lC				movil	r1, 600000
movhi		MOVe HIgh			rA = C  << 16			movhi	r1, 4
shl		SHift Left			rA = rB << rC			shl	r1, r2, r3
sli		Shift Left Immediate		rA = rB << C			sli	r1, r2, 4
shr		SHift Right (logical)		rA = rB >> rC			shr	r1, r2, r3
sri		Shift Right Immediate (logical)	rA = rB >> C			sri	r1, r2, 4
sra		SHift Right Arithmetical	rA = rB >> rC			sra	r1, r2, r3
srai		Shift Right Arithm. Immediate	rA = rB >> C			sri	r1, r2, 4
and		bitwise AND			rA = rB & rC			and	r1, r2, r3
andi		bitwise	AND Immediate		rA = rB & C			andi	r1, r2, 4
or		bitwise OR			rA = rB | rC			or	r1, r2, r3
ori		bitwise OR Immediate		rA = rB ^ C			ori	r1, r2, 4
xor		bitwise XOR			rA = rB ^ rC			xor	r1, r2, r3
xori		botwise XOR Immediate		rA = rB ^ C			xori	r1, r2, 4
not		NOT				rC = rC ^ -1			not	r1
br		BRanch				pc += C				br	label
brr		BRanch Register			pc += rC			brr	r1
jmp		JuMP				pc = C				jmp	label
jmpr		JuMP Register			pc = rC				jmpr	r1
beq		Branch if EQual			pc += C  if rA == rB		beq	r1, r2, label
bi		Branch If not zero		pc += C  if rA != r0		bi	r1, label
bne		Branch if Not Equal		pc += C	 if rA != rB		bne	r1, r2, label
bl		Branch if Less than		pc += C  if rA < rB		bl	r1, r2, label
ble		Branch if Less or Equal		pc += C  if rA <= rB		ble	r1, r2, label
call		CALL				r15 = pc, pc += C		call	label
callr		CALL Register			r15 = pc, pc = rC		call	r1
rd		ReaD				rA = mem(rB + C)		rd	r1, r2, label
wr		WRite				mem(rB+C) = rA			wr	r1, r2, label
push		PUSH to stack top		mem(r14) = rA, r14 = r14 + 1	push	r1
pop		POP stack top			r14 = r14 - 1, rA = mem(r14)	pop	r1
halt		HALT				halt execution			halt
nop		NO oPeration			nothing				nop
cmp		CoMPare				rA = -1 if rB < rC, rA = 0 if rB == rC, else rA = 1
flcmp		FLoating point CoMPare		rA = -1 if rB < rC, rA = 0 if rB == rC, else rA = 1 note: will only work if rB and rC are floating point numbers
int/inti	INTerrupt/INT. Immediate	if exceptions are enabled, this spawns an exception and copies the argument to register es (ea = pc, es = rC, pc = GLOBAL_IADRESS if GLOBAL_IENABLE & 1 == 1) 
eret		Exception RETurn		pc = ea, enables interrupts.	eret

Floating point instructions:
The floating point instructions operate on the floating point unit stacks. There are two stacks available.
The operands in the floating point operations are ordered like this: (top-1) * top, (top-1) - top, (top-1) / top, (top-1) + top.
note that both floating point stacks can perform operations simultaneously, and that the cpu does not have to wait for them to finish their operations, unless using the fpush/fpop instructions

Name:		Verbose:												Example:
		
fpush		push rA to floating point stack										fpush	r1
fpush1		push floating point 1 to floating point stack								fpush1
fpushn1		push floating point -1 to floating point stack								fpushn1
fpushpi		push floating point pi to floating point stack								fpushpi
fpop		rA = top of floating point stack									fpop	r1
fadd		pop two numbers from stack, perform addition, push result to stack					fadd
fsub		pop two numbers from stack, perform subtraction, push result to stack					fsub
fmul		pop two numbers from stack, perform multiplication, push result to stack				fmul
fdiv		pop two numbers from stack, perform division, push result to stack					fdiv
fsel0		select floating point stack 0										fsel0
fsel1		select floating point stack 1										fsel1
fmerge		adds the top number of stack 0 to top number of stack 1, result pushed to stack 0			fmerge
ftof		converts the integer at the top of the stack to a floating point number, and pushes on the stack	ftof
ftoi		converts the floating point number at the top of the stack to integer, and pushes on the stack		ftoi
fflush		removes all entries from the selected floating point stack and aborts all operations			fflush
fss		sets the high half word of rA to the selected stack, the low half word is set to the stack pointer	fss	r1

Extended instruction set:
please be aware that the assembler is not good at using these instructions yet. If you use a constant higher than 255
the assembler WILL NOT output a correct instruction

Name:		Verbose:			Result:								Example:

movb/movbr	MOVe Byte 			replace byte C/rC of rA with byte C/rC of rB			movb	r1, r2, 4
movhw/movhwr	MOVe Half-Word			replace half-word C/rC of rA with half-word C/rC of rB		movhw	r1, r2, 0
movbl/movblr	MOV Byte Low			replace byte C/rC of rA with byte 0 of rB			movbl	r1, r2, 4
movhwl/movhwlr	MOVe Half-Word Low		replace half-word C/rC of rA with half-word 0 of rB		movhwl	r1, r2, 0

The following instructions have not been fully tested yet, be careful when using them:
land		Logical AND			rA = 1 if rB !=0 and rC !=0, rA = 0 otherwise			land	r1, r2, r3
lor		Logical	OR			rA = 1 if rB !=0 or rC !=0,  rA = 0 otherwise			lor	r1, r2, r3
lxor		Logical XOR			rA = 1 if rB !=0 xor rC !=0, rA = 0 otherwise			lxor	r1, r2, r3
sbit		Set BIT				Sets Bit rC of rB to 1, stores in rA				sbit	r1, r2, r3
gbit		Get BIT				sets rA to 1 if bit rC of rB is 1, 0 otherwise			gbit	r1, r2, r3
sbitc		Set BIT Conditional		sets bit rC of rA to 1 if rB != 0				sbitc	r1, r2, r3
xbit		Xor BIT				toggles bit rC of rB, stores result in rA			xbit	r1, r2, r3
le		LEss than			sets rA to 1 if rB < rC, 0 otherwise				le	r1, r2, r3
leq		LEss than or eQual to		sets rA to 1 if rB <= rC, 0 otherwise				leq	r1, r2, r3
eq		EQual to			sets rA to 1 if rB == rC, 0 otherwise				eq	r1, r2, r3
neq		Not EQual to			sets rA to 1 if rB != rC, 0 otherwise				neq	r1, r2, r3
leu		LEss than Unsigned		same as le, assumes unsigned integers				leu	r1, r2, r3
lequ		LEss than or EQual to Unsigned	same as leq, assumes unsigned integers				lequ	r1, r2, r3
msb		Most Significant Bit		masks out the most significant bit of rC, stores in rA		msb	r1, r2
msbn		Most Significant Bit Numeric	sets rA to the index of the most significant bit in rC		msbn	r1, r2
lsb		Least Significant Bit		masks out the least significant bit in rC, stores in rA		lsb	r1, r2
lsbn		Least Significant Bit Numeric	sets rA to the index of the least significant bit in rC		lsbn	r1, r2


Interrupts:

in order to enable interrupts the memory at address 45, or "GLOBAL_IENABLE" if libLabel.txt is included, needs to be modified.
the different bits in the memory adress have the following effect:
bit	0:	enables interrupts to be queued
	1:	enables interrupt queue handling
	2+:	does nothing yet

when an interrupt occurs pc is immidiately set to the adress stored at address 44, or "GLOBAL_IADRESS", the interrupt id is copied to register es and pc is copied to register ea.
when the cpu jumps, bit 1 is immediately set to 0.
one interrupt can be handled per instruction executed.
256 interrupts can be queued at a time

built in interrupts:
interrupt id		cause
256			timer has flipped to 0
258			illegal instruction/division by 0


Assembler:

The ".meta", ".data" and ".text" sections:

All intructions for the assembler needs to follow one of these tags.
If something other than a comment is not surrounded by a tag, the compiler will give the error: "text outside of specified field"
You can have multiple instrances of the same section in each file.
When the code is compiled the fields are merged together in the order they appear, so the last .data section specified will be at the bottom of the .data section.
when using the #include keyword all fields are merged so that the #included fields are placed last

The ".meta" tag signifies that you are trying to tell the assembler how to compile your code. Within this field you can change these options:
PIC		default: off	will make a bunch of errors if you try to perform an operation that cannot be done if you don't know where the program is loaded
MAINJMP		default: on	will add the line "br main" in the beginning of the file. This will make the code start execution at the "main"-label declared #global
AUTOSTACK	default: on	will add the line "movi	sp, DATA_END" at the beginning of the file. This will automatically set the stack pointer to a good-ish position.
ALWERR		default: off	will make sure that if there is a warning somewhere, the warning will stop compilation. This probably does nothing at the moment
AUTLDR		default: on	does nothing at the moment.
the syntax is this:
.meta
PIC=FALSE
MAINJMP=TRUE

if an option is declared both true and false, the assembler will refuse to assemble the program.

the .text section contains all instructions. The syntax is described elsewhere
example:
.text
#global main
main:
	movi	r1, 1
	movi	r2, 2
	add	r1, r1, r2
	wr	r1, r0, GLOBAL_NUMPAD_OUT
	halt

the .data section contains all data the program needs
labels can still be used here
data can be declared as hexadecimal, binary, floating point or normal base 10 integers.
example:
.data
some_data:
0b10000	;binary number
0x10	;hexadecimal number
16	;base 10
16.0	;floating point

labels:

any line where the first word ends in ':' is a location, which can be used in, for example, the jmp instruction.
the jmp, jmpr, eret and callr instructions use the absolute rather than the relative value when modifying pc.
All other branching instructions instructions use pc-relative addressing
example:
	loop:
	nop
	jmp loop
will execute the nop and jmp instructions forever.

keywords:

#global	<label>
this keyword will make sure that the label will be accessable from all files that #include that file.
example:
	#global example
	example:
	...

#include <file>
will merge the file you specify with the current one. A file can only be #included once
example:
	#include example.txt

#allocate <number>
Will put number of zero - initialised integers where the #allocate statement was placed. It can only be placed in the .data section.

#string "<string>"
will put the specified string into memory in ascii format using one character per word. It will also add null-termination.
the string will be read from the first " to the last ", anything else will be regarded as a comment
the following escape sequences are supported:
	\n	line feed
	\a	bell
	\t	horizontal tab
	\0	null
	\\	backslash
	\"	"
	\f	form feed
	\r	carrige return
the string must be spacified across a single line
this keyword can only be placed in the .data section

#string_compact "<string>"
the same as #string, except it uses 4 chars per word instead of one
this makes strings consume less memory, at the expense of being slightly harder to use.
note that the example code expects the "#string"-strings

#macro <macroname> <parameters> {
<stuff>
}
will create a macroinstruction called macroname, using the declared ammount of parameters.
this macro can then be used as a normal instruction.
example:
#macro add3 rA rB rC {
	add	rA, rA, rB
	add	rA, rA, rC
}
add3	r1, r2, r3;this code works

the parameters specified in the macro definition are changed using a primitive
find-and-replace on the old parameters using the new, so if a macro is structured like this:
#macro foo rA rAA {
or this:
#macro foo r1 r2 r3 {
then the assembler might not be able to understand the instructions the macro produces, which can cause confusing error messages.
you can not use macroinstructions within macro declarations yet.

#define <defineName> <number>
will substitute <defineName> with <number> when used as a parameter
example:
#define foo 16
movi	r1, foo;equivalent to "movi	r1, 16"
.data
	foo;equivalent to "16"

#literal <constant> will place the constant evaluated as a literal in the program exactly where it is placed.
It is intended for putting arbitrary data into the .text section.
can only be used in the .text section.


Computer control panel:

To run a program on the cpu:
1: First write the program in a plain text file.
2: The file can then be assembled by using the command
3: "asm <filename.extension> [opt: <location>]" in the console. This creates a tape file with the name <filename.pct>
4: Run this file by opening up the tape drive interface (GUI->TAP) and inserting the tape (type filename into lower text field, press load).
5: Start the computer if you have not already.

All programs should be stored in <gamedir>/gamedata/progCom/plugins/PluginData/progCom

console commands:
print <offset>				prints the first 100 memory locations after the offset in the dev console (alt-f2)
clear					clears the console.
debug					toggle the emulators debug mode. This makes it run at cycle per frame and print everything it's doing to the dev console (Alt-f2).
step					if in debug mode, this will run the next instruction
asm <filename>	[opt: <location>]	will create a tape file (extension .pct) and save it into the plugindata folder. If the location is specified the program will be expected to be loaded there. Default (unspecified) is 256

explaination of the buttons at the top of the panel:

ON/OFF		turn the computer on and off
CTL		toggles whether the computer can control pitch/yaw/roll etc. on and off. It also controls staging and translation until I implement separate buttons for that.
TTL		toggles whether the computer may control the throttle.
RST		resets the computer
GUI		minimizes/maximises the computer ui panel
MON		displays/hides the monitor
KBD		displays/hides the keyboard
TAP		displays/hides the tape drive
	





