Skip to content

Commit

Permalink
Generators + Terra SOC Harness + Register slave (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshegarty committed Jun 15, 2018
1 parent 0604265 commit b8ea53d
Show file tree
Hide file tree
Showing 55 changed files with 3,292 additions and 848 deletions.
40 changes: 40 additions & 0 deletions examples/convgen.lua
@@ -0,0 +1,40 @@
local R = require "rigel"
R.export()
local SOC = require "soc"
local harness = require "harnessSOC"
local RS = require "rigelSimple"
local C = require "examplescommon"
require "generators".export()
require "types".export()

regs = SOC.axiRegsN(4,0):instantiate()

local ConvWidth = 4
local ConvRadius = ConvWidth/2

inSize = { 1920, 1080 }
padSize = { 1920+16, 1080+3 }

local conv = Module{ ar(u(8),ConvWidth,ConvWidth),
function(inp)
inp = Map{AddMSBs{24}}(inp)
local coeff = c{ar(u(32),ConvWidth,ConvWidth),{4, 14, 14, 4,
14, 32, 32, 14,
14, 32, 32, 14,
4, 14, 14, 4}}
local z = Zip(inp,coeff)
local out = Map{Mul}(z)
local res = Reduce{Add}(out)
return RemoveMSBs{24}(Rshift{8}(res))
end}

harness{
regs.start,
SOC.readBurst("1080p.raw",1920,1080,u(8),1),
HS{Pad{inSize,1,{8,8,2,1}}},
-- RS.HS(C.print(ar(u(8),1))),
HS{Linebuffer{padSize,1,{3,0,3,0}}},
HS{Map{conv}},
HS{Crop{padSize,1,{9,7,3,0}}},
SOC.writeBurst("out/convgen",1920,1080,u(8),1),
regs.done}
149 changes: 147 additions & 2 deletions examples/examplescommon.lua
Expand Up @@ -19,16 +19,60 @@ C.identity = memoize(function(A)
return identity
end)

C.print = memoize(function(A)
local identity = RM.lift( "print_"..J.verilogSanitize(tostring(A)), A, A, 0, function(sinp) return sinp end, function() return CT.print(A) end, "C.print")
return identity
end)

C.cast = memoize(function(A,B)
err(types.isType(A),"cast: A should be type")
err(types.isType(B),"cast: B should be type")
err( R.isBasic(A), "cast: A should be basic type. casting "..tostring(A).." to "..tostring(B) )
err( R.isBasic(B), "cast: B should be basic type. casting "..tostring(A).." to "..tostring(B) )
assert(A:isTuple()==false) -- not supported by terra
if terralib~=nil then
err(A:isTuple()==false, "C.cast: NYI - terra cast from '"..tostring(A).."' to '"..tostring(B).."'") -- not supported by terra
end
local docast = RM.lift( J.sanitize("cast_"..tostring(A).."_"..tostring(B)), A, B, 0, function(sinp) return S.cast(sinp,B) end, function() return CT.cast(A,B) end, "C.cast" )
return docast
end)

C.bitcast = memoize(function(A,B)
err(types.isType(A),"cast: A should be type")
err(types.isType(B),"cast: B should be type")
err( R.isBasic(A), "cast: A should be basic type. casting "..tostring(A).." to "..tostring(B) )
err( R.isBasic(B), "cast: B should be basic type. casting "..tostring(A).." to "..tostring(B) )

err(A:verilogBits()==B:verilogBits(), "TYPE SIZE NOT EQ: "..tostring(A).." - "..tostring(B))
local docast = RM.lift( J.sanitize("bitcast_"..tostring(A).."_"..tostring(B)), A, B, 0,
function(sinp)
local tmp = S.cast(sinp,types.bits(A:verilogBits()))
return S.cast(tmp,B)
end,
function() return CT.bitcast(A,B) end, "C.cast" )
return docast
end)

-- takes bits(N)[M] to bits(N*M)
C.flattenBits = memoize(function(ty)
if ty:isArray() and ty:arrayOver():isBits() then
return RM.lift( J.sanitize("FlattenBits_"..tostring(ty)), ty, types.bits(ty:verilogBits()), 0, function(sinp) return S.cast(sinp,types.bits(ty:verilogBits())) end, function() assert(false) end, "C.flattenBits" )
else
J.err( "NYI - flattenBits of type "..tostring(ty) )
end
end)

-- takes bits(N*M) to bits(M)[N]
C.partitionBits = memoize(function(ty, N)
J.err( type(N)=="number", "C.partitionBits: N must be number")
J.err( ty:verilogBits()%N==0, "C.partitionBits: N must device type bits")
if ty:isBits() then
local otype = types.array2d(types.bits(ty:verilogBits()/N),N)
return RM.lift( J.sanitize("PartitionBits_"..tostring(ty).."_"..tostring(N)), ty, otype, 0, function(sinp) return S.cast(sinp,otype) end, function() assert(false) end, "C.partitionBits" )
else
J.err( "NYI - partitionBits of type "..tostring(ty) )
end
end)

-- takes {T[N/2],T[N/2]} to T[N]
C.flatten2 = memoize(function(T,N)
err(types.isType(T),"cast: T should be type")
Expand Down Expand Up @@ -68,6 +112,33 @@ C.multiply = memoize(function(A,B,outputType)
return partial
end)

------------
-- return A*B as a darkroom FN. A,B are types
-- returns something of type outputType
C.multiplyConst = memoize(function(A,constValue)
err( types.isType(A), "C.multiply: A must be type")

local partial = RM.lift( J.sanitize("mult_const_A"..tostring(A).."_value"..tostring(constValue)), A, A, 1,
function(sinp) return sinp*S.constant(constValue,A) end,
function() return CT.multiplyConst(A,constValue) end,
"C.multiplyConst" )

return partial
end)


C.tokenCounter = memoize(function(A)
err( types.isType(A), "C.multiply: A must be type")

local partial = RM.lift( J.sanitize("tokencounter_A"..tostring(A)), A, A, 1,
function(sinp) assert(false) end,
function() return CT.tokenCounter(A) end,
"C.tokenCounter" )

partial.terraModule = CT.tokenCounter(A)
return partial
end)

------------
-- return A+B as a darkroom FN. A,B are types
-- returns something of type outputType
Expand Down Expand Up @@ -122,6 +193,62 @@ C.sub = memoize(function( A, B, outputType, async )
return partial
end)

-----------------------------
C.rshift = memoize(function(A,const)
err( types.isType(A),"C.rshift: type should be rigel type")
err( type(const)=="number" or const==nil,"C.rshift: const should be number or value")

if const~=nil then
-- shift by a const
J.err( A:isUint() or A:isInt(), "generators.Rshift: type should be int or uint, but is: "..tostring(A) )
local mod = RM.lift(J.sanitize("generators_rshift_const"..tostring(const).."_"..tostring(A)), A,nil,nil,
function(inp) return S.rshift(inp,S.constant(const,A)) end)
return mod
else
assert(false)
end
end)

-----------------------------
C.addMSBs = memoize(function(A,bits)
err( types.isType(A),"C.addMSBs: type should be rigel type")
err( type(bits)=="number","C.addMSBs: bits should be number or value")

local otype
if A:isUint() then
otype = types.uint(A.precision+bits)
elseif A:isInt() then
otype = types.int(A.precision+bits)
else
J.err( A:isUint() or A:isInt(), "generators.addMSBs: type should be int or uint, but is: "..tostring(A) )
end

local mod = RM.lift(J.sanitize("generators_addMSBs_"..tostring(bits).."_"..tostring(A)), A,nil,nil,
function(inp) return S.cast(inp,otype) end)
return mod
end)

-----------------------------
C.removeMSBs = memoize(function(A,bits)
err( types.isType(A),"C.removeMSBs: type should be rigel type")
err( type(bits)=="number","C.removeMSBs: bits should be number or value")

local otype
if A:isUint() then
J.err(A.precision>bits,"removeMSBs: can't remove all the bits! attempting to remove "..bits.." bits from "..tostring(A))
otype = types.uint(A.precision-bits)
elseif A:isInt() then
J.err(A.precision>bits,"removeMSBs: can't remove all the bits! attempting to remove "..bits.." bits from "..tostring(A))
otype = types.int(A.precision-bits)
else
J.err( A:isUint() or A:isInt(), "generators.removeMSBs: type should be int or uint, but is: "..tostring(A) )
end

local mod = RM.lift(J.sanitize("generators_removeMSBs_"..tostring(bits).."_"..tostring(A)), A,nil,nil,
function(inp) return S.cast(inp,otype) end)
return mod
end)

-----------------------------
C.select = memoize(function(ty)
err(types.isType(ty), "C.select error: input must be type")
Expand Down Expand Up @@ -160,6 +287,24 @@ C.valueToTrigger = memoize(function(ty)
function(sinp) end, nil,"C.valueToTrigger")
end)

-----------------------------
C.triggerToConstant = memoize(function(ty,value)
J.err( types.isType(ty), "C.triggerToConstant: type must be type")
return RM.lift( "TriggerToConstant_"..tostring(ty), types.null(), ty, 0,
function(sinp) return S.constant(value,ty) end, nil,"C.triggerToConstant")
end)

-----------------------------
-- take in 1 trigger, and write out N triggers (aka trigger upsample)
C.triggerUp = memoize(function(N)
J.err(type(N)=="number","triggerUp: input must be number")
local inp = R.input(R.HandshakeTrigger)
local val = RM.makeHandshake( RM.constSeq({0},types.uint(8),1,1,1), nil, true )(inp)
val = RM.upsampleXSeq(types.uint(8),1,N)(val)
val = RM.makeHandshake(C.valueToTrigger(types.array2d(types.uint(8),1)),nil,true)(val)
return RM.lambda("TriggerUp_"..tostring(N), inp,val)
end)

-----------------------------
C.packTap = memoize(function(A,ty,global)
return RM.lift( "PackTap_"..tostring(A).."_"..tostring(ty), A, types.tuple{A,ty}, 0,
Expand Down Expand Up @@ -1150,7 +1295,7 @@ function C.linearPipeline(t,modulename)
end

err(type(t)=="table" and J.keycount(t)==#t, "C.linearPipeline: input must be array")
for _,v in ipairs(t) do err(R.isFunction(v), "C.linearPipeline: input must be table of Rigel modules") end
for k,v in ipairs(t) do err(R.isFunction(v), "C.linearPipeline: input must be table of Rigel modules (idx "..k..")") end

local inp = R.input(t[1].inputType)
local out = inp
Expand Down
91 changes: 88 additions & 3 deletions examples/examplescommonTerra.t
@@ -1,10 +1,15 @@
local cstdlib = terralib.includec("stdlib.h")
local cstdio = terralib.includec("stdio.h")
local rigel = require "rigel"
local types = require "types"
local J = require "common"
local err = J.err
local MT = require "modulesTerra"

local data = macro(function(i) return `i._0 end)
local valid = macro(function(i) return `i._1 end)
local ready = macro(function(i) return `i._2 end)

CT={}

function CT.identity(A)
Expand All @@ -13,12 +18,68 @@ function CT.identity(A)
end
end

function CT.print(A)

local function doprint(A,symb)
assert(symb~=nil)

if A:isArray() then
local tab = {}
table.insert(tab,quote cstdio.printf("[") end)
for i=0,A:channels()-1 do
table.insert(tab,doprint(A:arrayOver(),`symb[i]))
if i~=A:channels()-1 then table.insert(tab,quote cstdio.printf(",") end) end
end
table.insert(tab,quote cstdio.printf("]") end)
return quote [tab] end
elseif A:isTuple() then
local tab = {}
table.insert(tab,quote cstdio.printf("{") end)
for i=1,#A.list do
table.insert(tab,doprint(A.list[i],`symb.["_"..(i-1)]))
if i~=#A.list then table.insert(tab,quote cstdio.printf(",") end) end
end
table.insert(tab,quote cstdio.printf("}") end)
return quote [tab] end
elseif A:isUint() or A:isInt() or A:isBits() then
return quote cstdio.printf("%d",symb) end
else
print(A)
assert(false)
end
end

return terra( a : &A:toTerraType(), out : &A:toTerraType() )
var aa = @a
[doprint(A,aa)]
cstdio.printf("\n")
@out = @a
end
end

function CT.cast(A,B)
err(types.isType(B), "examples common cast, B must be type")


if A:isBits() then
return terra( a : &A:toTerraType(), out : &B:toTerraType() )
@out = @[&B:toTerraType()](a)
end
elseif B:isBits() and A:isArray() and B:verilogBits()==A:verilogBits() then
return terra( a : &A:toTerraType(), out : &B:toTerraType() )
@out = @[&B:toTerraType()](a)
end
else
return terra( a : &A:toTerraType(), out : &B:toTerraType() )
@out = [B:toTerraType()](@a)
end
end
end

function CT.bitcast(A,B)
assert( terralib.sizeof(A:toTerraType()) == terralib.sizeof(B:toTerraType()) )
return terra( a : &A:toTerraType(), out : &B:toTerraType() )
@out = [B:toTerraType()](@a)
end
@out = @[&B:toTerraType()](a)
end
end

function CT.flatten2(T,N)
Expand Down Expand Up @@ -50,6 +111,30 @@ function CT.multiply(A,B,outputType)
end
end

function CT.multiplyConst(A,constValue)
return terra( a : &A:toTerraType(), out : &A:toTerraType() )
@out = [A:toTerraType()](@a)*[A:toTerraType()](constValue)
end
end

function CT.tokenCounter(A)
local struct TokenCounter { cnt:uint, ready:bool }
terra TokenCounter:reset() self.cnt=0 end
terra TokenCounter:process( a : &A:toTerraType(), out : &A:toTerraType() )
@out = @a

if valid(a) and self.ready then
self.cnt = self.cnt+1
cstdio.printf("CNT %d\n",self.cnt)
end
end
terra TokenCounter:calculateReady(readyDownstream:bool)
self.ready = readyDownstream
end

return MT.new(TokenCounter)
end

function CT.sum(A,B,outputType,async)
return terra( a : &tuple(A:toTerraType(),B:toTerraType()), out : &outputType:toTerraType() )
@out = [outputType:toTerraType()](a._0)+[outputType:toTerraType()](a._1)
Expand Down
1 change: 1 addition & 0 deletions examples/gold/convgen.bmp
1 change: 1 addition & 0 deletions examples/gold/soc_flip.bmp
Binary file removed examples/gold/soc_simple.bmp
Binary file not shown.
1 change: 1 addition & 0 deletions examples/gold/soc_simple.bmp

0 comments on commit b8ea53d

Please sign in to comment.