Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // Enum for coordinate axis
  3. //
  4. enum XY = X, Y
  5.  
  6. // Big syntax
  7. enum RGB is
  8.     R
  9.     G
  10.     B
  11. end enum
  12.  
  13. //
  14. // Resolution structure
  15. //
  16. struct Resolution with (Coord : type(default int(15, 0))) is
  17.  
  18.     var active_area : Coord[for enum XY]
  19.     var front_porch : Coord[for enum XY]
  20.     var back_porch  : Coord[for enum XY]
  21.     var sync        : Coord[for enum XY]
  22.     var total       : Coord[for enum XY]
  23.  
  24.     var req_clock_mhz  : ClockFrequency
  25.        
  26.     function sync_start(xy : XY) : Coord = @active_area[xy] + @front_porch[xy]
  27.     function sync_end(xy : XY) : Coord   = @sync_start(xy) + @sync[xy] - 1
  28.     function in_sync(xy : XY, pos : Coord) = @sync_start(xy, pos) <= pos <= @sync_end(xy, pos)
  29.     function calc_total(xy : XY) : Coord = @active_area[xy] + @front_porch[xy] + @back_porch[xy] + @sync_pulse[xy]
  30.     function in_screen(x,y) : bool = x < @active_area.X && y < @active_area.Y
  31.  
  32.     assertion CoordValid  is
  33.         // internally assert_no_overflow rewrites expression
  34.         // into the series of intermediate steps that store result in bigger int (i.e. a+b+c => i1 = a + b; i2 = i1+c)
  35.         // then it checks that no overflow occur
  36.         assert_no_overflow("Coord is large enough for X", @calc_total(X))
  37.         assert_no_overflow("Coord is large enough for Y", @calc_total(Y))
  38.     end assertion
  39.  
  40.     assertion totalsValid is
  41.         assert("total dots per horizontal line", @calcTotal(X) == @total.X)
  42.         assert("total lines per frame", @calcTotal(Y) == total.Y)
  43.     end assertion
  44.  
  45.     assertion syncsOrdered is
  46.         assert("X", @sync_end(X) > @sync_start(X))
  47.         assert("Y", @sync_end(Y) > @sync_start(Y))
  48.     end assertion
  49.  
  50.     assertion clockNonZero = @req_clock_mhz > 0
  51.  
  52.     assertion clockMatch(c : clock) is
  53.         assert("clock match", c.frequency == @clock)
  54.     end assertion
  55. end struct        
  56.    
  57.  
  58. const VGA_800_600_60 : Resolution is
  59.     active_area = {800, 600}
  60.     front_porch = {40,  1}
  61.     back_porch  = {88, 23}
  62.     sync        = {128, 4}
  63.     total       = {1056, 628}
  64.    
  65.     ensure assertion (ALL) // prove all assertions that don't take arguments(these will be proved later)
  66. end
  67.  
  68.  
  69. module vgaImpl
  70. with
  71.     res : Resolution
  72.     bitsPerChannel : integer[for enum RGB]
  73. signals    
  74.     clk <: clock // explicitly declare clock
  75.     red :> int(bitsPerChannel.R)
  76.     green : output(int(bitsPerChannel.G)) // different syntax for output
  77.     blue  :> int(bitsPerChannel.B)
  78.     hsync, vsync :> bool
  79.     x, y :> Optional(bool, isOutputPos))
  80. is
  81.     // ensure last assertion
  82.     // if there are assertions that take argument, that module which uses them,
  83.     // must do at least one call to instantiate the proof.
  84.     ensure res.clockMatch(clk)
  85.    
  86.     // registers for current coordinate    
  87.     var px : res.Coord(default 0) // set value on both (implicit) reset signal and in initial block
  88.     var py : res.Coord(default 0)
  89.  
  90.     // specifications
  91.     assertion h_sync_valid_active = assert(0 <= x < res.active_area.X  ==> hsync == false)
  92.     assertion h_sync_valid_front = assert(res.active_area.X <= x < res.active_area + res.front_porch.X  ==> hsync == false)
  93.     assertion h_sync_valid_sync = assert(res.in_sync(x, px) ==> hsync == true)
  94.     assertion h_sync_valid_back = assert(res.active_area.X + res.front_porch.X + res.sync.X <= x < res.total.X ==> hsync == false)
  95.  
  96.     assertion v_sync_valid_active = assert(0 <= y < res.active_area.Y  ==> vsync == false)
  97.     assertion v_sync_valid_front = assert(res.active_area.Y <= y < res.active_area + res.front_porch.Y  ==> vsync == false)
  98.     assertion v_sync_valid_sync = assert(res.in_sync(y, py) ==> vsync == true)
  99.     assertion v_sync_valid_back = assert(res.active_area.Y + res.front_porch.Y + res.sync.Y <= y < res.total.Y ==> vsync == false)
  100.  
  101.     assertion color_invisible_out_of_screen is
  102.         assert(!res.in_screen(px, py) ==> red == 0)
  103.         assert(!res.in_screen(px, py) ==> green == 0)
  104.         assert(!res.in_screen(px, py) ==> blue == 0)
  105.     end assertion
  106.  
  107.     assertion coords_valid is
  108.         assert(px < res.total.X)
  109.         assert(py < res.total.Y)
  110.     end assertion
  111.  
  112.     // Driver for coordinate
  113.     task Pixel is
  114.         if px + 1 == res.total.X {  
  115.             px = 0
  116.             if (py + 1 != res.total.Y)
  117.                 py = py + 1
  118.             else
  119.                 py = 0
  120.         } else {
  121.             px = px + 1
  122.         }
  123.     end task
  124.  
  125.     // Syncs driver
  126.     task Sync is
  127.         hsync = res.in_sync(X, px)
  128.         vsync = res.in_sync(Y, py)
  129.     end task
  130.    
  131.  
  132.     // Color driver    
  133.     task Color is
  134.         if ! res.in_screen(px, py) {
  135.             red = 0
  136.             green = 0
  137.             blue = 0
  138.         } else {
  139.             const x_even = px(3)
  140.             const y_even = py(3)
  141.             const output = x_even == y_even
  142.  
  143.             red = output ? red.max_unsigned_value() : 0
  144.             green = output ? green.max_unsigned_value() : 0
  145.             blue = output ? blue.max_unsigned_value() : 0
  146.         }
  147.     end task
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement