VerilogCPU/spi_encoder.cr

43 lines
1.6 KiB
Crystal

require "option_parser"
encode = ""
slave_pin = "{sim:/cpu_controller/spi_ss}"
data_pin = "{sim:/cpu_controller/spi_mosi}"
clock_pin = "{sim:/cpu_controller/spi_clk}"
unit = "ns"
start = 20
cycle = 40
OptionParser.parse! do |parser|
parser.banner = "Usage: parser [arguments]"
parser.on("-s STRING", "--string=STRING", "Sets the string to be encoded.") do |s|
encode = s
end
parser.on("-u UNIT", "--unit=UNIT", "select the time units.") do |u|
unit = u
end
parser.on("-c CYCLE", "--cycle=CYCLE", "select the clock cycle speed") do |c|
cycle = c.to_i? || 20
end
parser.on("-b BEGIN", "--begin=BEGIN", "select the beginning of the programming mode") do |b|
start = b.to_i? || 20
end
parser.on("-s SPIN", "--ss-pin=SPIN", "select the slave select pin") do |sp|
slave_pin = sp
end
parser.on("-d DPIN", "--data-pin=DPIN", "select data pin") do |dp|
data_pin = dp
end
parser.on("-p CPIN", "--clk-pin=CPIN", "select master clock pin") do |cp|
clock_pin = cp
end
end
STDOUT << "force " << slave_pin << " 0 @ " << start << unit << "\n"
encode.chars.each_with_index do |char, index|
STDOUT << "force " << clock_pin << " 1 @ " << (start + cycle * index) << unit << "\n"
STDOUT << "force " << clock_pin << " 0 @ " << (start + cycle * index + cycle / 2) << unit << "\n"
STDOUT << "force " << data_pin << " " << char << " @ " << (start + cycle * index) << unit << "\n"
end
STDOUT << "force " << slave_pin << " 1 @ " << (start + cycle * (encode.size + 1)) << unit << "\n"