Advertisement
Guest User

Untitled

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