I’m attempting to use Julia as a hardware description language (HDL).
The idea is somewhere between Verilog and Matlab, which Electrical Engineers are both readily familiar with.
Ideally, I would make the HDL as convenient to use as possible for the end-user, where Electrical Engineers are an idiosyncratic bunch when it comes to their programming languages.
Ideally, the syntax would be modeled very similarly to that of the existing verilog syntax.
As a first question, Is it possible to call a struct as a function?
I created a struct called BitNumber
struct BitNumber
val::Int
bit_end::UInt
bit_start::UInt
width::UInt
BitNumber(v,e,s,w,b) = new( (a=63-e+s; v=v<<a ; v>>a) ,e,s,w, bits)
end
it stores a value, and contains some information about the bits at use.
if I were to create an instance of the BitNumber, I’d like to be able to extract specific bits from the number with a simple syntax.
i.e.
a = BitNumber(value = 15, bits = 4)
# a is 4 bits wide and has value 2
b = a(2,1)
# b is 2 bits wide and has value 3
I also have overloaded the bits function that does exactly this:
import Base.bits
function bits(number::BitNumber, bit_end, bit_start)
n = number.val << (63 - bit_end + number.bit_start)
n = n >> (63 - bit_end + bit_start + number.bit_start)
return BitNumber(n, bit_end - bit_start)
end
a = BitNumber(value = 15, bits = 4)
# a is 4 bits wide and has value 2
b = bits(a, 2, 1)
Is there a means to either
-
Call an instantiated BitNumber as a function, and define that function however I choose
-
Contain a function inside the BitNumber struct that can access the BitNumber’s other info
i.e.
struct BitNumber
val::Int
bit_end::UInt
bit_start::UInt
width::UInt
the_bits::Function
BitNumber(v,e,s,w,b) = new( (a=63-e+s; v=v<<a ; v>>a) ,e,s,w, bits)
end
a = BitNumber(value = 15, bits = 4)
b = a.the_bits(1,2)
Am I just barking up the wrong tree?
Thank you!