Browse Source

basic definition of data structure

master
Michael Preisach 8 years ago
commit
c00c806c7f
  1. 90
      src/main.rs

90
src/main.rs

@ -0,0 +1,90 @@
use std::prelude;
use std::ops::*;
fn main() {
println!("Hello World!");
let mut s = Vec::new();
s.push(9);
println!("{}",s.pop().unwrap());
}
struct Cpu {
stack: Vec<u16>,
mem: Vec<u16>,
reg: Vec<u16>,
pc: Number,
}
impl Cpu {
fn new() -> Cpu {
Cpu {
stack: Vec::new(),
mem: Vec::with_capacity(32768),
reg: Vec::with_capacity(8),
pc: Number::new(),
}
}
}
//15Bit numbers for the ALU
#[derive(Eq, PartialEq, Copy, Clone)]
struct Number {
value: u16,
}
//Operator Overloading - Div not needed since it works without modification.
impl Add for Number {
type Output = Number;
fn add(self, other: Number) -> Number {
Number {
value: (self.value + other.value) % 32768,
}
}
}
impl Sub for Number {
type Output = Number;
fn sub(self, other: Number) -> Number {
Number {
value: (self.value - other.value) % 32768,
}
}
}
impl Mul for Number {
type Output = Number;
fn mul(self, other: Number) -> Number {
Number {
value: (self.value * other.value) % 32768,
}
}
}
impl Not for Number {
type Output = Number;
fn not(self) -> Number {
Number {
value: (!self.value) % 32768,
}
}
}
impl Number {
//little Endian ->
fn new() -> Number {
Number {
value: 0,
}
}
fn get_first_byte(&self) -> u8 {
(self.value & 0xFF) as u8
}
fn get_second_byte(&self) -> u8 {
((self.value >> 8) & 0x7F) as u8
}
fn set_bytes(&mut self, first: u8, second: u8) {
let mut newval:u16 = (second & 0x7F) as u16;
newval <<= 8;
self.value = newval + (first as u16);
}
}
Loading…
Cancel
Save