Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
83
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.     ensure res.clockMatch(clk)
  74.    
  75.     // registers for current coordinate    
  76.     var px : res.Coord(default 0) // on both reset and initial
  77.     var py : res.Coord(default 0)
  78.  
  79.     // specifications
  80.     assertion h_sync_valid_active = assert(0 <= x < res.active_area.X  ==> hsync == false)
  81.     assertion h_sync_valid_front = assert(res.active_area.X <= x < res.active_area + res.front_porch.X  ==> hsync == false)
  82.     assertion h_sync_valid_sync = assert(res.in_sync(x, px) ==> hsync == true)
  83.     assertion h_sync_valid_back = assert(res.active_area.X + res.front_porch.X + res.sync.X <= x < res.total.X ==> hsync == false)
  84.  
  85.     assertion v_sync_valid_active = assert(0 <= y < res.active_area.Y  ==> vsync == false)
  86.     assertion v_sync_valid_front = assert(res.active_area.Y <= y < res.active_area + res.front_porch.Y  ==> vsync == false)
  87.     assertion v_sync_valid_sync = assert(res.in_sync(y, py) ==> vsync == true)
  88.     assertion v_sync_valid_back = assert(res.active_area.Y + res.front_porch.Y + res.sync.Y <= y < res.total.Y ==> vsync == false)
  89.  
  90.     assertion color_invisible_out_of_screen is
  91.         assert(!res.in_screen(px, py) ==> red == 0)
  92.         assert(!res.in_screen(px, py) ==> green == 0)
  93.         assert(!res.in_screen(px, py) ==> blue == 0)
  94.     end assertion
  95.  
  96.     assertion coords_valid is
  97.         assert(x < res.total.X)
  98.         assert(y < res.total.Y)
  99.     end assertion
  100.  
  101.     // Driver for coordinate
  102.     task Pixel is
  103.         if x + 1 == res.total.X {  
  104.             x = 0
  105.             if (y + 1 != res.total)
  106.                 y = y + 1
  107.             else
  108.                 y = 0
  109.         } else {
  110.             x = x + 1
  111.         }
  112.     end task
  113.  
  114.     // Syncs driver
  115.     task Sync is
  116.         hsync = res.in_sync(X, px)
  117.         vsync = res.in_sync(Y, py)
  118.     end task
  119.    
  120.  
  121.     // Color driver    
  122.     task Color is
  123.         if ! res.in_screen(px, py) {
  124.             red = 0
  125.             green = 0
  126.             blue = 0
  127.         } else {
  128.             const x_even = px(3)
  129.             const y_even = py(3)
  130.             const output = x_even == y_even
  131.  
  132.             red = output ? red.max_unsigned_value() : 0
  133.             green = output ? green.max_unsigned_value() : 0
  134.             blue = output ? blue.max_unsigned_value() : 0
  135.         }
  136.     end task
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement