]>
Witch of Git - jade-mouse/blob - toolchain/src/cpu.rs
1 use crate::inst
::{Inst
, Reg
};
2 use std
::ops
::{Index
, IndexMut
};
19 pub fn new() -> Self {
23 reg
: RegFile
{ values
: [0; 4] },
24 memory
: vec
![0; 1 << 16],
28 pub fn reg(&self, reg
: Reg
) -> u8 {
32 pub fn mem(&self) -> &[u8] {
36 pub fn mem_mut(&mut self) -> &mut [u8] {
40 pub fn run_inst(&mut self, inst
: &Inst
) {
42 Inst
::Move(dst
, src
) => self.reg
[dst
] = self.reg
[src
],
48 impl Index
<Reg
> for RegFile
{
50 fn index(&self, index
: Reg
) -> &u8 {
52 Reg
::R0
=> &self.values
[0],
53 Reg
::R1
=> &self.values
[1],
54 Reg
::R2
=> &self.values
[2],
55 Reg
::R3
=> &self.values
[3],
60 impl IndexMut
<Reg
> for RegFile
{
61 fn index_mut(&mut self, index
: Reg
) -> &mut u8 {
63 Reg
::R0
=> &mut self.values
[0],
64 Reg
::R1
=> &mut self.values
[1],
65 Reg
::R2
=> &mut self.values
[2],
66 Reg
::R3
=> &mut self.values
[3],
71 impl std
::fmt
::Debug
for RegFile
{
72 fn fmt(&self, fmt
: &mut std
::fmt
::Formatter
) -> std
::fmt
::Result
{
73 fmt
.debug_struct("RegFile")
74 .field
("r0", &self[Reg
::R0
])
75 .field
("r1", &self[Reg
::R1
])
76 .field
("r2", &self[Reg
::R2
])
77 .field
("r3", &self[Reg
::R3
])
82 impl std
::fmt
::Debug
for Cpu
{
83 fn fmt(&self, fmt
: &mut std
::fmt
::Formatter
) -> std
::fmt
::Result
{
84 fmt
.debug_struct("Cpu")
85 .field
("init", &self.in
it
)
86 .field
("pc", &self.pc
)
87 .field
("reg", &self.reg
)
88 .field
("mem", &format_args
!("[...]"))