-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7ead0c
commit 2f18cb0
Showing
4 changed files
with
160 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import random | ||
import time | ||
import json | ||
from typing import Dict, Union | ||
|
||
MAX_CMDS = 15 | ||
ANS_MEM_LEN = 10 | ||
|
||
FormatType = Dict[str, Union[bool, str, int]] | ||
|
||
|
||
def format_gen(width: int) -> FormatType: | ||
"""Generates a format object for a bitvector of length `width`.""" | ||
return {"is_signed": False, "numeric_type": "bitnum", "width": width} | ||
|
||
|
||
def dump_json(): | ||
"""Prints a JSON representation of the data to stdout. | ||
The data itself is populated randomly, following certain rules: | ||
- It has three "memories": `commands`, `values`, and `ans_mem`. | ||
- The `commands` memory has MAX_CMDS items, which are 0, 1, or 2. | ||
- The `values` memory has MAX_CMDS items: random values between 0 and 100. | ||
- The `ans_mem` memory has ANS_MEM_LEN items, all zeroes. | ||
- Each memory has a `format` field, which is a format object for a bitvector. | ||
""" | ||
commands = { | ||
"commands": { | ||
"data": [random.randint(0, 2) for _ in range(MAX_CMDS)], | ||
# The `commands` memory has MAX_CMDS items, which are 0, 1, or 2. | ||
"format": format_gen(2), | ||
} | ||
} | ||
values = { | ||
"values": { | ||
"data": [random.randint(0, 100) for _ in range(MAX_CMDS)], | ||
# The `values` memory has MAX_CMDS items: random values between 0 and 100. | ||
"format": format_gen(32), | ||
} | ||
} | ||
ans_mem = { | ||
"ans_mem": { | ||
"data": [0 for _ in range(ANS_MEM_LEN)], | ||
# The `ans_mem` memory has ANS_MEM_LEN items, all zeroes. | ||
"format": format_gen(32), | ||
} | ||
} | ||
|
||
print(json.dumps(commands | values | ans_mem, indent=2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
random.seed(5) | ||
dump_json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import sys | ||
import json | ||
|
||
|
||
def parse_json(): | ||
"""Effectively the opposite of `data_gen`: | ||
Given a JSON file formatted for Calyx purposes, parse it into its two lists: | ||
- The `commands` memory, which has MAX_CMDS items. | ||
- The `values` memory, which has MAX_CMDS items. | ||
Returns the two lists. | ||
""" | ||
|
||
# The JSON file is piped to us in stdin. | ||
data = json.load(sys.stdin) | ||
commands = data["commands"]["data"] | ||
values = data["values"]["data"] | ||
return commands, values | ||
|
||
|
||
def operate_fifo(commands, values): | ||
"""Given the three lists, operate a FIFO routine. | ||
- Read the comammands list in order. | ||
- When the value is 0, we "pop" the FIFO and write the value to the answer memory. | ||
- When it is 1, we "peek" into the FIFO and write the value to the answer memory. | ||
- When it is 2, we push the coressponding item in the `values` list to the FIFO. | ||
In the end, we return the answer memory. | ||
""" | ||
fifo = [] | ||
ans = [] | ||
for cmd, val in zip(commands, values): | ||
if cmd == 0: | ||
if len(fifo) == 0: | ||
break | ||
ans.append(fifo.pop(0)) | ||
elif cmd == 1: | ||
if len(fifo) == 0: | ||
break | ||
ans.append(fifo[0]) | ||
elif cmd == 2: | ||
fifo.append(val) | ||
# Pad the answer memory with zeroes until it is of length ANS_MEM_LEN. | ||
ans += [0] * (10 - len(ans)) | ||
return ans | ||
|
||
|
||
def dump_json(commands, values, ans_mem): | ||
"""Prints a JSON representation of the data to stdout.""" | ||
payload = { | ||
"ans_mem": ans_mem, | ||
"commands": commands, | ||
"values": values, | ||
} | ||
print(json.dumps(payload, indent=2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
commands, values = parse_json() | ||
ans = operate_fifo(commands, values) | ||
dump_json(commands, values, ans) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
{ | ||
"ans_mem": [ | ||
100, | ||
100, | ||
101, | ||
102, | ||
103, | ||
104, | ||
0, | ||
0, | ||
47, | ||
47, | ||
47, | ||
31, | ||
31, | ||
69, | ||
13, | ||
73, | ||
0, | ||
0 | ||
], | ||
"commands": [ | ||
2, | ||
1, | ||
2, | ||
2, | ||
0, | ||
2, | ||
2, | ||
1, | ||
2, | ||
2, | ||
2, | ||
2, | ||
0, | ||
1, | ||
0, | ||
2, | ||
0, | ||
0, | ||
0 | ||
], | ||
"values": [ | ||
100, | ||
0, | ||
101, | ||
102, | ||
0, | ||
103, | ||
104, | ||
105, | ||
106, | ||
107, | ||
108, | ||
0, | ||
0, | ||
0, | ||
0 | ||
47, | ||
60, | ||
31, | ||
48, | ||
69, | ||
13, | ||
73, | ||
31, | ||
1, | ||
93, | ||
27, | ||
52, | ||
35, | ||
23, | ||
98 | ||
] | ||
} |