Firmware for MNT ZZ9000 graphics and ARM coprocessor card for Amiga computers.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

video_formatter.v 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. `timescale 1ns / 1ps
  2. /*
  3. * MNT ZZ9000 Amiga Graphics and Coprocessor Card Firmware
  4. * Video Stream Formatter
  5. *
  6. * Copyright (C) 2019, Lukas F. Hartmann <lukas@mntre.com>
  7. * MNT Research GmbH, Berlin
  8. * https://mntre.com
  9. *
  10. * More Info: https://mntre.com/zz9000
  11. *
  12. * SPDX-License-Identifier: GPL-3.0-or-later
  13. * GNU General Public License v3.0 or later
  14. *
  15. * https://spdx.org/licenses/GPL-3.0-or-later.html
  16. *
  17. */
  18. module video_formatter(
  19. input [31:0] m_axis_vid_tdata,
  20. input m_axis_vid_tlast,
  21. output m_axis_vid_tready,
  22. input [0:0] m_axis_vid_tuser,
  23. input m_axis_vid_tvalid,
  24. input m_axis_vid_aclk,
  25. input aresetn,
  26. input dvi_clk,
  27. output reg dvi_hsync,
  28. output reg dvi_vsync,
  29. output reg dvi_active_video,
  30. output reg [31:0] dvi_rgb,
  31. // control inputs for setting palette, width/height, scaling
  32. input [31:0] control_data,
  33. input [7:0] control_op,
  34. input control_interlace
  35. );
  36. localparam OP_COLORMODE=1;
  37. localparam OP_DIMENSIONS=2;
  38. localparam OP_PALETTE=3;
  39. localparam OP_SCALE=4;
  40. localparam OP_VSYNC=5;
  41. localparam OP_MAX=6;
  42. localparam OP_HS=7;
  43. localparam OP_VS=8;
  44. localparam OP_THRESH=9;
  45. localparam OP_POLARITY=10;
  46. localparam OP_RESET=11;
  47. localparam OP_UNUSED1=12;
  48. localparam OP_SPRITEXY=13;
  49. localparam OP_SPRITE_ADDR=14;
  50. localparam OP_SPRITE_DATA=15;
  51. localparam CMODE_8BIT=0;
  52. localparam CMODE_16BIT=1;
  53. localparam CMODE_32BIT=2;
  54. localparam CMODE_15BIT=4;
  55. reg [11:0] screen_width; // = 720;
  56. reg [11:0] screen_height; // = 576;
  57. reg scale_x = 0;
  58. reg scale_y = 1; // amiga boots in 640x256, so double the resolution vertically
  59. reg [31:0] palette[255:0];
  60. reg [2:0] colormode = CMODE_32BIT;
  61. reg vsync_request = 0;
  62. reg sync_polarity = 1; // negative polarity
  63. reg [15:0] screen_h_max; //= 864;
  64. reg [15:0] screen_v_max; //= 625;
  65. reg [15:0] screen_h_sync_start; //= 732;
  66. reg [15:0] screen_h_sync_end; //= 796;
  67. reg [15:0] screen_v_sync_start; //= 581;
  68. reg [15:0] screen_v_sync_end; //= 586;
  69. localparam MAXWIDTH=1280; // 1920?!?
  70. reg [31:0] line_buffer[MAXWIDTH-1:0];
  71. // (input) vdma state
  72. reg [3:0] next_input_state = 0;
  73. reg [11:0] inptr = 0;
  74. reg ready_for_vdma = 0;
  75. assign m_axis_vid_tready = ready_for_vdma;
  76. reg [11:0] counter_x = 0; // vga domain
  77. reg [11:0] counter_y = 0; // vga domain
  78. reg [11:0] need_line_fetch = 0; // vga domain
  79. reg [11:0] need_line_fetch_reg = 0;
  80. reg [11:0] need_line_fetch_reg2 = 0;
  81. reg [11:0] need_line_fetch_reg3 = 0;
  82. reg [11:0] last_line_fetch = 1;
  83. wire [31:0] pixin = m_axis_vid_tdata;
  84. wire pixin_valid = m_axis_vid_tvalid;
  85. wire pixin_end_of_line = m_axis_vid_tlast;
  86. wire pixin_framestart = m_axis_vid_tuser[0];
  87. reg scale_y_effective;
  88. reg need_frame_sync; // vga domain
  89. reg need_frame_sync_reg; // fetch domain
  90. // sprite
  91. localparam SPRITE_W = 32;
  92. localparam SPRITE_H = 48;
  93. localparam SPRITE_SIZE = SPRITE_W*SPRITE_H;
  94. reg [23:0] sprite_buffer[SPRITE_SIZE-1:0];
  95. reg [11:0] sprite_addr_in = 0;
  96. reg [11:0] sprite_x = 0;
  97. reg [11:0] sprite_y = 0;
  98. reg [11:0] vga_sprite_x = 0; // vga domain
  99. reg [11:0] vga_sprite_y = 0; // vga domain
  100. reg [11:0] sprite_px = 0; // vga domain
  101. reg [23:0] sprite_pix; // vga domain
  102. reg sprite_on = 0; // vga domain
  103. always @(posedge m_axis_vid_aclk)
  104. begin
  105. if (~aresetn) begin
  106. ready_for_vdma <= 0;
  107. next_input_state <= 0;
  108. inptr <= 0;
  109. end
  110. need_frame_sync_reg <= need_frame_sync;
  111. need_line_fetch_reg <= need_line_fetch; // sync to clock domain
  112. need_line_fetch_reg2 <= need_line_fetch_reg>>scale_y_effective; // line duplication
  113. scale_y_effective <= control_interlace ? 0 : scale_y;
  114. if (pixin_valid && ready_for_vdma) begin
  115. line_buffer[inptr] <= pixin;
  116. // disabling this makes the picture go wild
  117. if (pixin_framestart) // we might have missed the frame start
  118. inptr <= 1;
  119. else if (pixin_end_of_line) // next after this is the first pixel of the line (0)
  120. inptr <= 0;
  121. else
  122. inptr <= inptr + 1'b1;
  123. end
  124. case (next_input_state)
  125. 4'h0: begin
  126. // wait for start of frame
  127. ready_for_vdma <= 1;
  128. if (pixin_framestart)
  129. next_input_state <= 4'h3;
  130. end
  131. 4'h1: begin
  132. // reading from vdma
  133. last_line_fetch <= need_line_fetch_reg2;
  134. if (pixin_valid && pixin_end_of_line) begin
  135. ready_for_vdma <= 0;
  136. next_input_state <= 4'h2;
  137. end else
  138. ready_for_vdma <= 1; // moved here
  139. end
  140. 4'h2: begin
  141. // we've read more than enough of this line, wait until it's time for the next
  142. if (vsync_request) begin
  143. next_input_state <= 4'h0;
  144. end
  145. else if (need_line_fetch_reg2!=last_line_fetch) begin
  146. // time to read the next line
  147. next_input_state <= 4'h1;
  148. //ready_for_vdma <= 1; // from here
  149. end
  150. end
  151. 4'h3: begin
  152. // we are at frame start, wait for the first line of video output
  153. ready_for_vdma <= 0;
  154. // line_fetch_reg2 == 0
  155. if (need_frame_sync_reg==1) begin
  156. next_input_state <= 4'h2;
  157. end
  158. end
  159. endcase
  160. end
  161. reg [31:0] control_data_in;
  162. reg [7:0] control_op_in;
  163. reg control_interlace_in;
  164. reg [31:0] control_data_in2;
  165. reg [7:0] control_op_in2;
  166. reg control_interlace_in2;
  167. // control input
  168. always @(posedge m_axis_vid_aclk)
  169. begin
  170. control_op_in2 <= control_op;
  171. control_op_in <= control_op_in2;
  172. control_data_in2 <= control_data;
  173. control_data_in <= control_data_in2;
  174. control_interlace_in2 <= control_interlace;
  175. control_interlace_in <= control_interlace_in2;
  176. if (next_input_state==0) begin
  177. vsync_request <= 0;
  178. end
  179. if (control_interlace_in != control_interlace) begin
  180. vsync_request <= 1;
  181. end
  182. case (control_op_in)
  183. OP_PALETTE: palette[control_data_in[31:24]] <= control_data_in[23:0];
  184. OP_DIMENSIONS: begin
  185. screen_height <= control_data_in[31:16];
  186. screen_width <= control_data_in[15:0];
  187. end
  188. OP_SCALE: begin
  189. scale_x <= control_data_in[0];
  190. scale_y <= control_data_in[1];
  191. end
  192. OP_COLORMODE: colormode <= control_data_in[1:0]; // FIXME
  193. OP_VSYNC: vsync_request <= 1; //control_data[0];
  194. OP_MAX: begin
  195. screen_v_max <= control_data_in[31:16];
  196. screen_h_max <= control_data_in[15:0];
  197. end
  198. OP_HS: begin
  199. screen_h_sync_start <= control_data_in[31:16];
  200. screen_h_sync_end <= control_data_in[15:0];
  201. end
  202. OP_VS: begin
  203. screen_v_sync_start <= control_data_in[31:16];
  204. screen_v_sync_end <= control_data_in[15:0];
  205. end
  206. OP_THRESH: begin
  207. end
  208. OP_POLARITY: begin
  209. sync_polarity <= control_data_in[0];
  210. end
  211. OP_RESET: begin
  212. sync_polarity <= 1;
  213. screen_h_max <= 864;
  214. screen_v_max <= 625;
  215. screen_h_sync_start <= 732;
  216. screen_h_sync_end <= 796;
  217. screen_v_sync_start <= 581;
  218. screen_v_sync_end <= 586;
  219. scale_x <= 0;
  220. scale_y <= 1;
  221. screen_width <= 720;
  222. screen_height <= 576;
  223. colormode <= CMODE_32BIT;
  224. end
  225. OP_SPRITEXY: begin
  226. sprite_y <= control_data_in[31:16];
  227. sprite_x <= control_data_in[15:0];
  228. end
  229. OP_SPRITE_ADDR: begin
  230. sprite_addr_in <= control_data_in[11:0];
  231. end
  232. OP_SPRITE_DATA: begin
  233. sprite_buffer[sprite_addr_in] <= control_data_in[23:0];
  234. end
  235. endcase
  236. end
  237. reg [31:0] palout;
  238. reg [11:0] vga_v_rez;
  239. reg [11:0] vga_h_rez;
  240. reg [11:0] vga_v_max;
  241. reg [11:0] vga_h_max;
  242. reg [11:0] vga_h_sync_start;
  243. reg [11:0] vga_h_sync_end;
  244. reg [11:0] vga_v_sync_start;
  245. reg [11:0] vga_v_sync_end;
  246. reg [11:0] counter_scanout;
  247. reg [2:0] vga_colormode;
  248. reg [11:0] vga_h_rez_shifted;
  249. reg [11:0] vga_v_rez_shifted;
  250. reg vga_scale_x = 0;
  251. reg [31:0] pixout;
  252. reg [7:0] pixout8;
  253. reg [15:0] pixout16;
  254. reg [31:0] pixout32;
  255. reg [31:0] pixout32_dly;
  256. reg [31:0] pixout32_dly2;
  257. wire [7:0] red16 = {pixout16[4:0], pixout16[4:2]};
  258. wire [7:0] green16 = {pixout16[10:5], pixout16[10:9]};
  259. wire [7:0] blue16 = {pixout16[15:11], pixout16[15:13]};
  260. wire [7:0] red15 = {pixout16[4:0], pixout16[4:2]};
  261. wire [7:0] green15 = {pixout16[9:5], pixout16[9:7]};
  262. wire [7:0] blue15 = {pixout16[14:10], pixout16[14:12]};
  263. reg [3:0] counter_scanout_step; // = 0;
  264. reg [3:0] counter_subpixel = 0;
  265. reg vga_sync_polarity = 0;
  266. always @(posedge dvi_clk) begin
  267. vga_h_rez <= screen_width;
  268. vga_v_rez <= screen_height;
  269. vga_h_max <= screen_h_max;
  270. vga_v_max <= screen_v_max;
  271. vga_h_sync_start <= screen_h_sync_start; // + 4
  272. vga_h_sync_end <= screen_h_sync_end; // + 4
  273. vga_v_sync_start <= screen_v_sync_start;
  274. vga_v_sync_end <= screen_v_sync_end;
  275. vga_scale_x <= scale_x;
  276. vga_colormode <= colormode;
  277. vga_sync_polarity <= sync_polarity;
  278. if (counter_y == 0) begin
  279. vga_sprite_x <= sprite_x;
  280. vga_sprite_y <= sprite_y;
  281. end
  282. // FIXME there is some non-determinism in the relationship
  283. // between this process and the fetching process
  284. // depending on when a new screen is launched, there can be
  285. // 1 row of wrap-around
  286. /*
  287. pipelines (4 clocks):
  288. linebuf pixout32 pixout32_dly pixout32_dly2 pixout
  289. linebuf pixout32 pixout16 pixout32_dly pixout
  290. linebuf pixout32 pixout8 palout pixout
  291. */
  292. case ({vga_scale_x,counter_subpixel[2:0]})
  293. 4'b0011: pixout8 <= pixout32[31:24];
  294. 4'b0000: pixout8 <= pixout32[23:16];
  295. 4'b0001: pixout8 <= pixout32[15:8];
  296. 4'b0010: pixout8 <= pixout32[7:0];
  297. 4'b1111: pixout8 <= pixout32[31:24];
  298. 4'b1000: pixout8 <= pixout32[31:24];
  299. 4'b1001: pixout8 <= pixout32[23:16];
  300. 4'b1010: pixout8 <= pixout32[23:16];
  301. 4'b1011: pixout8 <= pixout32[15:8];
  302. 4'b1100: pixout8 <= pixout32[15:8];
  303. 4'b1101: pixout8 <= pixout32[7:0];
  304. 4'b1110: pixout8 <= pixout32[7:0];
  305. endcase
  306. case ({vga_scale_x,counter_subpixel[1:0]})
  307. 3'b001: pixout16 <= {pixout32[23:16],pixout32[31:24]};
  308. 3'b000: pixout16 <= {pixout32[7:0] ,pixout32[15:8] };
  309. 3'b100: pixout16 <= {pixout32[23:16],pixout32[31:24]};
  310. 3'b111: pixout16 <= {pixout32[23:16],pixout32[31:24]};
  311. 3'b110: pixout16 <= {pixout32[7:0] ,pixout32[15:8] };
  312. 3'b101: pixout16 <= {pixout32[7:0] ,pixout32[15:8] };
  313. endcase
  314. case ({vga_scale_x,vga_colormode})
  315. 4'b0000: counter_scanout_step <= 3; // 8 bit
  316. 4'b1000: counter_scanout_step <= 7;
  317. 4'b0001: counter_scanout_step <= 1; // 16 bit
  318. 4'b1001: counter_scanout_step <= 3;
  319. 4'b0010: counter_scanout_step <= 0; // 32 bit
  320. 4'b1010: counter_scanout_step <= 1;
  321. //4'b0100: counter_scanout_step <= 1; // 15 bit
  322. //4'b1100: counter_scanout_step <= 3;
  323. endcase
  324. if (counter_x>vga_h_rez) begin
  325. counter_scanout <= 0;
  326. counter_subpixel <= counter_scanout_step;
  327. end else begin
  328. if (counter_subpixel == 0) begin
  329. counter_subpixel <= counter_scanout_step;
  330. counter_scanout <= counter_scanout + 1'b1;
  331. end else
  332. counter_subpixel <= counter_subpixel - 1'b1;
  333. end
  334. pixout32 <= line_buffer[counter_scanout];
  335. if (vga_colormode==CMODE_16BIT)
  336. // 16 bit 5r6g5b
  337. pixout32_dly <= {8'b0,blue16,green16,red16};
  338. //else if (vga_colormode==CMODE_15BIT)
  339. // // 15 bit 5r5g5b for shapeshifter
  340. // pixout32_dly <= {8'b0,blue15,green15,red15};
  341. else
  342. pixout32_dly <= pixout32;
  343. pixout32_dly2 <= pixout32_dly;
  344. palout <= palette[pixout8];
  345. case (vga_colormode)
  346. CMODE_8BIT: pixout <= palout;
  347. CMODE_16BIT: pixout <= pixout32_dly;
  348. CMODE_32BIT: pixout <= pixout32_dly2;
  349. endcase
  350. sprite_pix <= sprite_buffer[sprite_px];
  351. if (counter_y >= vga_sprite_y && counter_y < (vga_sprite_y+SPRITE_H)
  352. && counter_x >= vga_sprite_x && counter_x < (vga_sprite_x+SPRITE_W)) begin
  353. sprite_on <= 1;
  354. sprite_px <= sprite_px + 1;
  355. end else begin
  356. sprite_on <= 0;
  357. end
  358. dvi_rgb <= (sprite_on && sprite_pix!='hff00ff) ? sprite_pix : pixout;
  359. if (counter_x > vga_h_max) begin
  360. counter_x <= 0;
  361. if (counter_y > vga_v_max) begin
  362. counter_y <= 0;
  363. sprite_px <= 0;
  364. end else begin
  365. counter_y <= counter_y + 1'b1;
  366. end
  367. end else begin
  368. counter_x <= counter_x + 1'b1;
  369. end
  370. if (counter_x==vga_h_rez) begin
  371. if (counter_y<vga_v_rez-1'b1)
  372. need_line_fetch <= counter_y + 1'b1;
  373. else
  374. need_line_fetch <= 0;
  375. end
  376. // signal synchronization point to fetch process
  377. if (counter_x<8 && counter_y==vga_v_sync_start)
  378. need_frame_sync <= 1;
  379. else
  380. need_frame_sync <= 0;
  381. if (counter_x>=vga_h_sync_start && counter_x<vga_h_sync_end)
  382. dvi_hsync <= 1^vga_sync_polarity;
  383. else
  384. dvi_hsync <= 0^vga_sync_polarity;
  385. if (counter_y>=vga_v_sync_start && counter_y<vga_v_sync_end)
  386. dvi_vsync <= 1^vga_sync_polarity;
  387. else
  388. dvi_vsync <= 0^vga_sync_polarity;
  389. // 4 clocks pipeline delay
  390. vga_h_rez_shifted <= vga_h_rez+4;
  391. if (counter_y<vga_v_rez && counter_x==4)
  392. dvi_active_video <= 1;
  393. if (counter_x==vga_h_rez_shifted)
  394. dvi_active_video <= 0;
  395. end
  396. endmodule