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.

458 lines
13KB

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