Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
rsim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
rsim
Commits
548c65d7
Commit
548c65d7
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
wip (broken)
parent
c5d9a4d7
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main.rs
+24
-42
24 additions, 42 deletions
src/main.rs
src/signal.rs
+57
-39
57 additions, 39 deletions
src/signal.rs
with
81 additions
and
81 deletions
src/main.rs
+
24
−
42
View file @
548c65d7
...
...
@@ -67,68 +67,50 @@ use rsim::*;
use
std
::
cell
::
Cell
;
#[derive(Debug,
Clone)]
struct
S
<
'a
,
const
N
:
usize
>
{
i
:
&
'a
[
Cell
<
bool
>
],
}
impl
<
'a
,
const
N
:
usize
>
S
<
'a
,
N
>
{
fn
new
(
a
:
&
'a
mut
[
bool
;
N
])
->
Self
{
let
cell_slice
:
&
'a
Cell
<
[
bool
]
>
=
Cell
::
from_mut
(
a
);
Self
{
i
:
cell_slice
.as_slice_of_cells
(),
}
}
}
// two input mux
struct
Mux
<
'a
,
const
N
:
usize
>
{
ins
:
(
S
<
'a
,
N
>
,
S
<
'a
,
N
>
),
sel
:
&
'a
S
<
'a
,
1
>
,
out
:
&
'a
S
<
'a
,
N
>
,
ins
:
(
S
ignal
<
'a
,
N
>
,
S
ignal
<
'a
,
N
>
),
sel
:
&
'a
S
ignal
<
'a
,
1
>
,
out
:
&
'a
S
ignal
<
'a
,
N
>
,
}
impl
<
'a
,
const
N
:
usize
>
Eval
for
Mux
<
'a
,
N
>
{
fn
eval
(
&
self
)
->
()
{
match
self
.sel.i
[
0
]
.get
()
{
false
=>
self
.out.i
[
0
]
.set
(
self
.ins
.0
.i
[
0
]
.get
()),
true
=>
self
.out.i
[
0
]
.set
(
self
.ins
.1
.i
[
0
]
.get
()),
let
sel
=
self
.sel
[
0
]
.get
();
for
(
i
,
o
)
in
self
.out
.iter
()
.enumerate
()
{
o
.set
(
match
sel
{
false
=>
self
.ins
.0
[
i
]
.get
(),
true
=>
self
.ins
.1
[
i
]
.get
(),
})
}
}
}
fn
main
()
{
let
slice
=
&
mut
[
false
,
false
];
let
cell_slice
:
&
Cell
<
[
bool
]
>
=
Cell
::
from_mut
(
slice
);
let
slice_cell
:
&
[
Cell
<
bool
>
]
=
cell_slice
.as_slice_of_cells
();
println!
(
"{}"
,
slice_cell
[
0
]
.get
());
slice_cell
[
0
]
.set
(
true
);
println!
(
"{}"
,
slice_cell
[
0
]
.get
());
let
mut
sd
=
[
true
];
let
s
=
S
::
new
(
&
mut
sd
);
println!
(
"{}"
,
s
.i
[
0
]
.get
());
s
.i
[
0
]
.set
(
false
);
println!
(
"{}"
,
s
.i
[
0
]
.get
());
let
mut
in1
=
[
false
];
let
mut
in2
=
[
true
];
let
mut
in1
=
[
false
,
true
];
let
mut
in2
=
[
true
,
false
];
let
mut
sel
=
[
false
];
let
sel
=
S
::
new
(
&
mut
sel
);
let
mut
out
=
[
false
];
let
sel
=
S
ignal
::
new
(
&
mut
sel
);
let
mut
out
=
[
false
,
false
];
let
out
=
&
S
::
new
(
&
mut
out
);
let
out
=
&
S
ignal
::
new
(
&
mut
out
);
let
m
=
Mux
{
ins
:
(
S
::
new
(
&
mut
in1
),
S
::
new
(
&
mut
in2
)),
ins
:
(
S
ignal
::
new
(
&
mut
in1
),
S
ignal
::
new
(
&
mut
in2
)),
sel
:
&
sel
,
out
,
};
println!
(
"m {:?}"
,
m
.out
);
println!
(
"m.out {:b}"
,
m
.out
);
m
.eval
();
sel
[
0
]
.set
(
true
);
m
.eval
();
sel
.i
[
0
]
.set
(
true
);
println!
(
"m.out {:b}"
,
m
.out
);
// sel[0].set(false);
sel
.store
([
false
]);
sel
.store_slice
(
&
[
false
]);
m
.eval
();
println!
(
"m {:
?
}"
,
m
.out
);
println!
(
"m
.out
{:
b
}"
,
m
.out
);
}
// #[derive(Debug)]
// struct Mux<'a, const I: usize, const S: usize, const N: usize> {
...
...
This diff is collapsed.
Click to expand it.
src/signal.rs
+
57
−
39
View file @
548c65d7
...
...
@@ -5,49 +5,79 @@ use std::ops::{BitAnd, BitOr, Deref, Index};
use
std
::
string
::
String
;
#[derive(Debug,
PartialEq,
Clone)]
pub
struct
Signal
<
const
N
:
usize
>
{
pub
struct
Signal
<
'a
,
const
N
:
usize
>
{
// we should probably have set/get instead of exposing store
pub
store
:
Cell
<
[
bool
;
N
]
>
,
pub
store
:
&
'a
[
Cell
<
bool
>
]
,
}
impl
<
const
N
:
usize
>
Signal
<
N
>
{
pub
fn
new
()
->
Self
{
impl
<
'a
,
const
N
:
usize
>
Signal
<
'a
,
N
>
{
pub
fn
new
(
arr
:
&
'a
mut
[
bool
;
N
])
->
Self
{
let
cell_slice
:
&
'a
Cell
<
[
bool
]
>
=
Cell
::
from_mut
(
arr
);
Self
{
store
:
C
ell
::
new
([
false
;
N
]
),
store
:
c
ell
_slice
.as_slice_of_cells
(
),
}
}
// pub fn set(&mut self, i: usize, b: bool) {
// self.store[i] = b;
// }
pub
fn
load
(
&
self
)
->
[
bool
;
N
]
{
let
mut
a
=
[
false
;
N
];
for
(
i
,
v
)
in
self
.store
.iter
()
.enumerate
()
{
a
[
i
]
=
v
.get
()
}
a
}
pub
fn
store
(
&
self
,
a
:
[
bool
;
N
])
{
for
(
i
,
v
)
in
self
.store
.iter
()
.enumerate
()
{
v
.set
(
a
[
i
])
}
}
pub
fn
store_slice
(
&
self
,
a
:
&
[
bool
])
{
for
(
i
,
v
)
in
self
.store
.iter
()
.enumerate
()
{
v
.set
(
a
[
i
])
}
}
}
// impl<const N: usize>
fmt::Binary
for Signal<N> {
// fn f
mt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
// let mut s =
String::new()
;
// for i in
self.store.get().iter().rev()
{
//
s.push(if *i { '1' } else { '0' });
// impl<
'a,
const N: usize>
From<u8>
for Signal<
'a,
N> {
// fn f
rom(u: u8) -> Self
{
// let mut
b
s =
[false; N]
;
// for i in
0..8
{
//
bs[i] = 1 << i != 0
// }
//
write!(f, "{}",
s)
//
Signal::new(&mut b
s)
// }
// }
//
impl<const N: usize> Deref for Signal<N> {
//
type Target = [Cell<bool>
; N
];
impl
<
'a
,
const
N
:
usize
>
Deref
for
Signal
<
'a
,
N
>
{
type
Target
=
[
Cell
<
bool
>
];
// fn deref(&self) -> Self::Target {
// (&self.store).as_slice_of_cells()
// }
// }
fn
deref
(
&
self
)
->
&
Self
::
Target
{
self
.store
}
}
impl
<
'a
,
const
N
:
usize
>
fmt
::
Binary
for
&
Signal
<
'a
,
N
>
{
fn
fmt
(
&
self
,
f
:
&
mut
fmt
::
Formatter
<
'_
>
)
->
fmt
::
Result
{
let
mut
s
=
String
::
new
();
for
i
in
self
.store
.iter
()
.rev
()
{
s
.push
(
if
i
.get
()
{
'1'
}
else
{
'0'
});
}
write!
(
f
,
"{}"
,
s
)
}
}
//
impl<const N: usize> Index<usize> for Signal<N> {
//
type Output =
bool
;
impl
<
'a
,
const
N
:
usize
>
Index
<
usize
>
for
Signal
<
'a
,
N
>
{
type
Output
=
Signal
<
'a
,
1
>
;
// #[inline]
// fn index(self, index: usize) -> Self::Output {
// self.store.get()[index]
// }
// }
#[inline]
fn
index
(
&
self
,
index
:
usize
)
->
Self
::
Output
{
Signal
{
store
:
&
[
self
.store
[
index
]
.clone
()],
}
}
}
// operations on signal
// impl<const N: usize> BitAnd for &Signal<N> {
...
...
@@ -80,18 +110,6 @@ impl<const N: usize> Signal<N> {
// }
// }
// impl<const N: usize> From<u8> for Signal<N> {
// fn from(u: u8) -> Self {
// let mut bs = [false; N];
// for i in 0..8 {
// bs[i] = u & (1 << i) != 0
// }
// Signal {
// store: Cell::new(bs),
// }
// }
// }
// impl<const N: usize> From<[bool; N]> for Signal<N> {
// fn from(a: [bool; N]) -> Self {
// Signal {
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment