はので、ここで[OK]を、私がやろうとしたものです。
// 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エミュレータを作成することです。ここで私はすべてのファイルを1つのファイルに表示していますが、実際にはCPUのエミュレータ全体が、バスが実装されていることを知らなくても書き込むことができます。私はこれが意味をなさないことを願っています。それは私にはありません。私は再びRustを楽しんでいます!
**優れた** [* The Rust Programming Language *](https://doc.rust-lang.org/stable/book/secondedition/)これらの入門的な質問の多くをカバーしています。これは[traitsの章](https://doc.rust-lang.org/stable/book/secondedition/ch10-02-traits.html)で説明されています。 – Shepmaster
私は本の第1版を読んだが、第2版があることに気付かず、最後にそれを使用して以来、少し変わったので、私はそれを読む必要がある。私の主な問題は、継承ベースの言語からRustのコンポジションベースのデザインに切り替えることだと思います。 –