Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flaviusb/cb1d84a89005a5b42eaa6fcf3dd2dc4c to your computer and use it in GitHub Desktop.
Save flaviusb/cb1d84a89005a5b42eaa6fcf3dd2dc4c to your computer and use it in GitHub Desktop.
do-with-in-example
// This version imagines a define_chip macro that has been enhanced with do_with_in; I'm looking at how to do this through providing an annotation macro to wrap other proc macros
define_chip! {
$(fn stall(name, args: {}, check: []) {
CheckStall <- 0 (instruction @ super::super::Instruction::$name::(super::super::Instructions::$name($args)), mems) => {
$(if $check is not empty {
let ($(for $j in $check { stalled_$j, })) = ($(for $j in $check { get_stalled($j, mems) }));
if !($(for $k in $check { $k | }) false) {
return super::super::Work::Computing { progress: 0, instruction, mem: mems };
} else {
let mut blocker = vec!();
$(for $i in $check {
if stalled_$i {
blocker.push($i)
}
})
}
return super::super::Work::StalledComputing {
forward_by: 0, progress: 0, instruction, mem: mems, waiting_on: vec!(blocker) };
} else {
super::super::Work::Computing { progress: 0, instruction, mem: mems }
} ) }
}
)
# lotus_chip
## Misc
- Instruction width: 32
// Many parts elided for the example
## Pipeline
- fetch in Fetch = super::super::fetch
- decode in Decode = super::super::Instructions::decode
- check_stall in CheckStall: super::super::MachineState -> super::super::Work
## Instructions
Nop, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0, $(stall name = {Nop}), "Nop."
Sleep, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1, $(stall name = {Sleep}), "Sleep."
T, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 a:[u; 8], $(stall name = {T}, args = {a: u8}), "T."
F, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 a:[u; 8], $(stall name = {F}), args = {a: u8}) "F."
Not, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 a:[u; 8], $(stall name = {Not}, args = {a: u8}, check = [{a}]), "¬."
AddIS32Sat, 1 0 1 0 0 0 0 1 a:[u; 8] b:[u; 8] c:[u; 8], $(stall name = {AddIS32Sat}, args = {a: u8, b: u8, c: u8,}, check = [{a}, {b}]), "Add 32-bit integer, saturating."
}
do_with_in!{
sigil: $
do $(fn stall(name, args: {}, check: []) {
CheckStall <- 0 (instruction @ super::super::Instruction::$name::(super::super::Instructions::$name($args)), mems) => {
%(if $check is not empty {
let ($(for $j in $check { stalled_$j, })) = ($(for $j in $check { get_stalled($j, mems) }));
if !($(for $k in $check { $k | }) false) {
return super::super::Work::Computing { progress: 0, instruction, mem: mems };
} else {
let mut blocker = vec!();
$(for $i in $check {
if stalled_$i {
blocker.push($i)
}
})
}
return super::super::Work::StalledComputing {
forward_by: 0, progress: 0, instruction, mem: mems, waiting_on: vec!(blocker) };
} else {
super::super::Work::Computing { progress: 0, instruction, mem: mems }
} ) }
}
)
define_chip! {
# lotus_chip
## Misc
- Instruction width: 32
// Many parts elided for the example
## Pipeline
- fetch in Fetch = super::super::fetch
- decode in Decode = super::super::Instructions::decode
- check_stall in CheckStall: super::super::MachineState -> super::super::Work
## Instructions
Nop, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0, $(stall name = {Nop}), "Nop."
Sleep, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1, $(stall name = {Sleep}), "Sleep."
T, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 a:[u; 8], $(stall name = {T}, args = {a: u8}), "T."
F, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 a:[u; 8], $(stall name = {F}), args = {a: u8}) "F."
Not, 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 a:[u; 8], $(stall name = {Not}, args = {a: u8}, check = [{a}]), "¬."
AddIS32Sat, 1 0 1 0 0 0 0 1 a:[u; 8] b:[u; 8] c:[u; 8], $(stall name = {AddIS32Sat}, args = {a: u8, b: u8, c: u8,}, check = [{a}, {b}]), "Add 32-bit integer, saturating."
}
}
@flaviusb
Copy link
Author

(Example for using define_chip!, from https://github.com/flaviusb/fantasy-cpu-emulator )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment