-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a ROT13 workshop example #273
Open
mibus
wants to merge
4
commits into
im-tomu:master
Choose a base branch
from
mibus:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,83 @@ | ||
#!/usr/bin/env python3 | ||
# This variable defines all the external programs that this module | ||
# relies on. lxbuildenv reads this variable in order to ensure | ||
# the build will finish without exiting due to missing third-party | ||
# programs. | ||
LX_DEPENDENCIES = ["icestorm", "yosys", "nextpnr-ice40"] | ||
#LX_CONFIG = "skip-git" # This can be useful for workshops | ||
|
||
# Import lxbuildenv to integrate the deps/ directory | ||
import os,os.path,shutil,sys,subprocess | ||
sys.path.insert(0, os.path.dirname(__file__)) | ||
import lxbuildenv | ||
|
||
# Disable pylint's E1101, which breaks completely on migen | ||
#pylint:disable=E1101 | ||
|
||
from migen import * | ||
from migen.genlib.resetsync import AsyncResetSynchronizer | ||
|
||
from litex.soc.integration.soc_core import SoCCore | ||
from litex.soc.integration.builder import Builder | ||
from litex.soc.interconnect.csr import AutoCSR, CSRStatus, CSRStorage, CSRField | ||
|
||
from litex_boards.partner.targets.fomu import BaseSoC, add_dfu_suffix | ||
|
||
from valentyusb.usbcore import io as usbio | ||
from valentyusb.usbcore.cpu import dummyusb | ||
|
||
import argparse | ||
|
||
# ROT13 input CSR. Doesn't need to do anything special. | ||
class FomuROT13In(Module, AutoCSR): | ||
def __init__(self): | ||
self.csr = CSRStorage(8) | ||
|
||
# ROT13 output CSR, plus the wiring to automatically create the output from the input CSR. | ||
class FomuROT13Out(Module, AutoCSR): | ||
def __init__(self, rot13_in): | ||
self.csr = CSRStorage(8) | ||
self.sync += If( # A-M, a-m | ||
(rot13_in.csr.storage >= ord('A')) & (rot13_in.csr.storage <= ord('M')) | (rot13_in.csr.storage >= ord('a')) & (rot13_in.csr.storage <= ord('m')), | ||
self.csr.storage.eq(rot13_in.csr.storage + 13) | ||
).Elif( # N-Z, n-z | ||
(rot13_in.csr.storage >= ord('N')) & (rot13_in.csr.storage <= ord('Z')) | (rot13_in.csr.storage >= ord('n')) & (rot13_in.csr.storage <= ord('z')), | ||
self.csr.storage.eq(rot13_in.csr.storage - 13) | ||
).Else( | ||
self.csr.storage.eq(rot13_in.csr.storage) | ||
) | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Build Fomu Main Gateware") | ||
parser.add_argument( | ||
"--seed", default=0, help="seed to use in nextpnr" | ||
) | ||
parser.add_argument( | ||
"--placer", default="heap", choices=["sa", "heap"], help="which placer to use in nextpnr" | ||
) | ||
parser.add_argument( | ||
"--board", choices=["evt", "pvt", "hacker"], required=True, | ||
help="build for a particular hardware board" | ||
) | ||
args = parser.parse_args() | ||
|
||
soc = BaseSoC(args.board, pnr_seed=args.seed, pnr_placer=args.placer, usb_bridge=True) | ||
|
||
# Create a CSR-based ROT13 input and output, export the CSRs | ||
rot13_in = FomuROT13In() | ||
soc.submodules.fomu_rot13_in = rot13_in | ||
soc.submodules.fomu_rot13_out = FomuROT13Out(rot13_in) | ||
soc.add_csr("fomu_rot13_in") | ||
soc.add_csr("fomu_rot13_out") | ||
|
||
builder = Builder(soc, | ||
output_dir="build", csr_csv="build/csr.csv", | ||
compile_software=False) | ||
vns = builder.build() | ||
soc.do_exit(vns) | ||
add_dfu_suffix(os.path.join('build', 'gateware', 'top.bin')) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could do with some more explanation :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comments in 5a3c933, let me know if that's what you're looking for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap starting to look good! I do think you might want a comment which says "we only rotate the alphabetic characters" (what about the numbers? :-) and maybe a link to the ROT13 wikipedia page?
Could be good to add a test bench in the future too....
From https://en.wikipedia.org/wiki/ROT13 it looks like
memfrob()
in the GNU C library could be an excellent target for this type of acceleration :-PThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linked the wiki page, added a brief explanation of the overall cipher, mentioned that it's limited to the English alphabet.