Skip to content

Instantly share code, notes, and snippets.

@blackle
Created September 17, 2021 04:01
Show Gist options
  • Save blackle/150b1af2f732277acc3c7700e83bc742 to your computer and use it in GitHub Desktop.
Save blackle/150b1af2f732277acc3c7700e83bc742 to your computer and use it in GitHub Desktop.
that time I tried making a fantasy console (circa 2015)
=== the cpu itself ===
registers:
unsigned, 16 bit.
user accessible:
ax,bx,cx,dx,ex,fx,gx,hx
inaccessible:
ip (instruction pointer)
sp (stack pointer)
instruction types:
instructions are 16 bits
r-type:
opcode r1 r2 r3 resv
5----- 3- 3- 3- 2---
ra-type:
opcode r1 r2 address5
5----- 3- 3- 5-------
i-type:
opcode r1 address8
5----- 3- 8-------
j-type:
opcode address11
5----- 11-------
instructions:
legend:
(name)/(type)/(bytecode)/(implemeted?)
add /r/00000/y
r1 + r2 -> r3
//resv = 1, add carry?
mult/r/00001/y
r1 * r2 -> r3
sub /r/00010/y
r1 - r2 -> r3
//resv = 1, subtract borrow?
div /r/00011/y
r1 / r2 -> r3
to do mod you'd just div then mult and subtract
this is better than that goddamn division function
and /r/00100/y
r1 & r2 -> r3
or /r/00101/y
r1 | r2 -> r3
xor /r/00110/y
r1 ^ r2 -> r3
not /r/00111/y
!r1 -> r2
shl /r/01000/y
r1 << 1 -> r2
shr /r/01001/y
r1 >> 1 -> r2
addi/ra/01010/y
r1 + address5 -> r2
subi/ra/01011/y
r1 - address5 -> r2
mov /r/01100/y
if resv == 0: r1 -> r2
if resv == 1: r1 -> r2, r2 -> r1 (swap)
if resv == 2: sp -> r1
if resv == 3: r1 -> sp
movl/i/01101/y
address8 -> lower 8 bits of r1
movh/i/01110/y
address8 -> upper 8 bits of r1
ldhi/ra/01111/y
load value at r2 + address5 into high part of r1
sthi/ra/10000/y
set value at r2 + address5 to the high part of r1
ldlo/ra/10001/y
load value at r2 + address5 into low part of r1
stlo/ra/10010/y
set value at r2 + address5 to the low part of r1
jmp /j/10011/y
jump to ip + address11
jmpi/ra/10100/y
jump to r1 + address5
jge /ra/10101/y
if r1 >= r2 then jump to ip + address5
jle /ra/10110/y
if r1 <= r2 then jump to ip + address5
jez /ra/10111/y
if r1 & r2 == 0 then jump to ip + address5
the case when r1 = r2 means "jump when zero"
this is useful for bitmasking
jeq /ra/11000/y
if r1 == r2 then jump to ip + address5
push/r/11001/y
push r1 onto stack
pop /r/11010/y
pop r1 off of stack
call/r/11011/y
call function at address r1
jal /j/11100/y
call function at address11
ret /r/11101/y
return
____/_/11110/n (unallocated)
syscall/j/11111/y
call kernel with trapcode = address11
calling conventions:
parameters are pushed onto the stack like any sane system.
the return address is added by the call/jal instruction
caller save:
ax - because it will be overwritten anyway
ex
fx
gx
hx
callee save:
bx
dx
cx
=== system calls ===
all system calls are simulated by the interpreter (currently thats ./main)
when it comes to errno, I'm thinking of adding a system call to check its
value, sys_errno maybe. then we'll have errno as a field in ProcessState.
this is ok because there is no shared memory concurrency, so no locks on that.
having a system call for errno is a suggested alternative in the posix standard
== implemented ==
0 sys_exit
1 sys_fork
2 sys_read
3 sys_write
== unimplemented ==
12 sys_time
19 sys_getpid
24 sys_stime
29 sys_utime
42 sys_times
== not going to implement ==
// these are page things
44 sys_brk
89 old_mmap
90 sys_munmap
189 sys_vfork (not posix)
== requires filetable ==
== requires pipes ==
40 sys_dup
41 sys_pipe
53 sys_ioctl
54 sys_fcntl
== requires virtual disk ==
4 sys_open
5 sys_close
7 sys_creat
8 sys_link
9 sys_unlink
10 sys_execve
11 sys_chdir
13 sys_mknod
14 sys_chmod
15 sys_lchown
17 sys_stat
18 sys_lseek
20 sys_mount
21 sys_oldumount
27 sys_fstat
37 sys_rename
38 sys_mkdir
39 sys_rmdir
51 sys_umount
== requires signals ==
6 sys_waitpid
36 sys_kill
47 sys_signal
== requires uids/gids/perm system ==
22 sys_setuid
23 sys_getuid
45 sys_setgid
46 sys_getgid
48 sys_geteuid
49 sys_getegid
== idk ==
25 sys_ptrace
26 sys_alarm
28 sys_pause
32 sys_access
33 sys_nice
35 sys_sync
50 sys_acct
56 sys_setpgid
58 sys_olduname
59 sys_umask
60 sys_chroot
61 sys_ustat
62 sys_dup2
63 sys_getppid
64 sys_getpgrp
65 sys_setsid
66 sys_sigaction
67 sys_sgetmask
68 sys_ssetmask
69 sys_setreuid
70 sys_setregid
71 sys_sigsuspend
72 sys_sigpending
73 sys_sethostname
74 sys_setrlimit
75 sys_getrlimit
76 sys_getrusage
77 sys_gettimeofday
78 sys_settimeofday
79 sys_getgroups
80 sys_setgroups
81 old_select
82 sys_symlink
83 sys_lstat
84 sys_readlink
85 sys_uselib
86 sys_swapon
87 sys_reboot
88 old_readdir
91 sys_truncate
92 sys_ftruncate
93 sys_fchmod
94 sys_fchown
95 sys_getpriority
96 sys_setpriority
98 sys_statfs
99 sys_fstatfs
100 sys_ioperm
101 sys_socketcall
102 sys_syslog
103 sys_setitimer
104 sys_getitimer
105 sys_newstat
106 sys_newlstat
107 sys_newfstat
108 sys_uname
109 sys_iopl
110 sys_vhangup
111 sys_idle
112 sys_vm86old
113 sys_wait4
114 sys_swapoff
115 sys_sysinfo
116 sys_ipc (*Note)
117 sys_fsync
118 sys_sigreturn
119 sys_clone
120 sys_setdomainname
121 sys_newuname
122 sys_modify_ldt
123 sys_adjtimex
124 sys_mprotect
125 sys_sigprocmask
126 sys_create_module
127 sys_init_module
128 sys_delete_module
129 sys_get_kernel_syms
130 sys_quotactl
131 sys_getpgid
132 sys_fchdir
133 sys_bdflush
134 sys_sysfs
135 sys_personality
137 sys_setfsuid
138 sys_setfsgid
139 sys_llseek
140 sys_getdents
141 sys_select
142 sys_flock
143 sys_msync
144 sys_readv
145 sys_writev
146 sys_getsid
147 sys_fdatasync
148 sys_sysctl
149 sys_mlock
150 sys_munlock
151 sys_mlockall
152 sys_munlockall
153 sys_sched_setparam
154 sys_sched_getparam
155 sys_sched_setscheduler
156 sys_sched_getscheduler
157 sys_sched_yield
158 sys_sched_get_priority_max
159 sys_sched_get_priority_min
160 sys_sched_rr_get_interval
161 sys_nanosleep
162 sys_mremap
163 sys_setresuid
164 sys_getresuid
165 sys_vm86
166 sys_query_module
167 sys_poll
168 sys_nfsservctl
169 sys_setresgid
170 sys_getresgid
171 sys_prctl
172 sys_rt_sigreturn
173 sys_rt_sigaction
174 sys_rt_sigprocmask
175 sys_rt_sigpending
176 sys_rt_sigtimedwait
177 sys_rt_sigqueueinfo
178 sys_rt_sigsuspend
179 sys_pread
180 sys_pwrite
181 sys_chown
182 sys_getcwd
183 sys_capget
184 sys_capset
185 sys_sigaltstack
186 sys_sendfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment