好了,所以這裏是我要怎樣做。
// bus controls reads and writes to/from emulated cpu
// a trait allows me to define the interface the cpu expects
pub trait Bus {
fn read(&self, size: Size, address: u32) -> u32;
fn write(&self, size: Size, address: u32, data: u32);
}
// must have an object to implement the bus on
pub struct Mem {} // this can be filled out later
// implement Bus trait for our Mem
impl Bus for Mem {
fn read(&self, size: Size, address: u32) -> u32 { unimplemented!() }
fn write(&self, size: Size, address: u32, data: u32) { unimplemented!() }
}
// here is our cpu struct with a generic Bus trait object in it
pub struct M68K<A: Bus> {
pub d: [u32; 8],
pub a: [u32; 8],
pub x: bool, // extend
pub n: bool, // negative
pub z: bool, // zero
pub v: bool, // overflow
pub c: bool, // carry
pub bus: A // here is the Bus trait object
}
// when we implement our cpu we can use the Bus trait methods, this allows
// multiple cpu's to be created with the same or different Bus objects.
// possibly connect 2 cpu's to the same Bus object?
impl M68K<Mem> {
pub fn step(&self, cycles: u32) {
let x = self.bus.read(Size::Byte, 0xffff);
}
}
此代碼尚未完成。我的主要目標是爲我自己的項目創建一個易於使用的68k cpu仿真器。在這裏,我將所有內容顯示在一個文件中,但實際上整個cpu模擬器現在可以在沒有任何有關總線實現的知識的情況下編寫。我希望這是有道理的,它對我有用,我再次享受着魯斯特!
這是**高度建議**,你閱讀**優秀** [*鏽編程語言*](https://doc.rust-lang.org/stable/book/second-edition/)其中涵蓋了很多這些介紹性問題。這在[特性章節](https://doc.rust-lang.org/stable/book/second-edition/ch10-02-traits.html)中有介紹。 – Shepmaster
我讀過這本書的第一版,沒有意識到有第二版,我必須閱讀它,因爲Rust自從我上次使用它之後發生了一些變化。我想我的主要問題是從基於繼承的語言切換到Rust的基於合成的設計。 –