modVM/
lib.rs

1#![allow(dead_code)]
2#![allow(unused_variables)]
3
4use std::sync::mpsc::{self, *};
5
6pub mod machines;
7
8pub use self::machines::Machine;
9
10/* The Main datatypes for modular_vm */
11
12#[derive(Debug)]
13pub enum Query<BASE> {
14    LoadRequest(BASE),
15    SaveRequest(BASE, BASE),
16}
17
18#[derive(Debug)]
19pub enum Response<BASE> {
20    Data(BASE),
21    Good,
22    Fail(BASE),
23}
24
25pub struct TwoWayChannel<B, T> {
26    out: Sender<B>,
27    back: Receiver<T>
28}
29
30impl<I, O> TwoWayChannel<I, O> {
31    pub fn construct() -> (TwoWayChannel<I, O>, TwoWayChannel<O, I>) {
32        let (senderout, receiverout) = mpsc::channel();
33        let (senderin, receiverin) = mpsc::channel();
34
35        (
36            TwoWayChannel {
37                out: senderout,
38                back: receiverin,
39            },
40
41            TwoWayChannel {
42                out: senderin,
43                back: receiverout,
44            }
45        )
46    }
47
48    pub fn send(&self, data: I) -> Result<(), SendError<I>> {
49        self.out.send(data)
50    }
51
52    pub fn recv(&self) -> Result<O, RecvError> {
53        self.back.recv()
54    }
55
56    pub fn try_recv(&self) -> Result<O, TryRecvError> {
57        self.back.try_recv()
58    }
59
60    pub fn iter(&self) -> Iter<O> {
61        self.back.iter()
62    }
63
64    pub fn try_iter(&self) -> TryIter<O> {
65        self.back.try_iter()
66    }
67
68    pub fn query(&self, data: I) -> Result<O, RecvError> {
69        self.send(data).unwrap();
70        self.recv()
71    }
72}
73
74pub struct Metadata {
75    pub model: String,
76}
77
78pub type FrontEnd<BASE> = TwoWayChannel<Query<BASE>, Response<BASE>>;
79
80pub type BackEnd<BASE> = TwoWayChannel<Response<BASE>, Query<BASE>>;
81
82/* Peripherals and Processors for a normal machine */
83
84pub trait Peripheral<BASE> {
85    fn handle(&mut self, incoming: Query<BASE>) -> Result<Response<BASE>, BASE>;
86
87    fn metadata(&self) -> Metadata {
88        Metadata {
89            model: String::from("Peripheral"),
90        }
91    }
92
93    fn cycle(&mut self) -> Result<(), BASE> {
94        Ok(())
95    }
96
97    fn boot(&mut self) -> Result<(), BASE> {
98        Ok(())
99    }
100
101    fn halt(&mut self) -> Result<(), BASE> {
102        Ok(())
103    }
104}
105
106pub trait Processor<BASE> {
107    fn exe_ins(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE>;
108
109    fn metadata(&self) -> Metadata {
110        Metadata {
111            model: String::from("Processor"),
112        }
113    }
114
115    fn boot(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE> {
116        Ok(())
117    }
118
119    fn halt(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE> {
120        Ok(())
121    }
122}
123
124/* Processors for a Processor Network machine */
125
126pub trait ProcessorNode<BASE> {
127    fn exe_ins(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE>;
128
129    fn metadata(&self) -> Metadata {
130        Metadata {
131            model: String::from("ProcessorNode"),
132        }
133    }
134
135    fn handle(&mut self, incoming: Query<BASE>) -> Result<Response<BASE>, BASE>;
136
137    fn boot(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE> {
138        Ok(())
139    }
140
141    fn halt(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE> {
142        Ok(())
143    }
144}
145
146/* Nodes for a Node Network machine */
147
148pub trait Node<BASE> {
149    fn cycle(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE>;
150
151    fn handle(&mut self, incoming: Query<BASE>) -> Result<Response<BASE>, BASE>;
152
153    fn metadata(&self) -> Metadata {
154        Metadata {
155            model: String::from("Node"),
156        }
157    }
158
159    fn boot(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE> {
160        Ok(())
161    }
162
163    fn halt(&mut self, channels: &Vec<FrontEnd<BASE>>) -> Result<(), BASE> {
164        Ok(())
165    }
166}