spriteflyer_sprite_rtl.vhd 13.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
--------------------------------------------------------------------------------
-- Fault InJection Instrumenter (FIJI)
-- https://embsys.technikum-wien.at/projects/vecs/fiji
--
-- Copyright (C) 2017 Christian Fibich <fibich@technikum-wien.at>
-- Copyright (C) 2017 Stefan Tauner <tauner@technikum-wien.at>
--
-- Copyright and related rights are licensed under the Solderpad Hardware
-- License, Version 0.51 (the "License"); you may not use this file except
-- in compliance with the License. You may obtain a copy of the License at
-- http://solderpad.org/licenses/SHL-0.51. Unless required by applicable
-- law or agreed to in writing, software, hardware and materials
-- distributed under this License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or
-- implied. See the License for the specific language governing
-- permissions and limitations under the License.
--
-- See the LICENSE file for more details.
--
-- Description:
--  TMR_VGA architecture file
--------------------------------------------------------------------------------


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
library ieee;
    use ieee.std_logic_1164.all;
    use ieee.numeric_std.all;

library work;
    use work.spriteflyer_pkg.all;

entity spriteflyer_sprite is
    generic (
        G_RED_WIDTH   : natural;
        G_GREEN_WIDTH : natural;
        G_BLUE_WIDTH  : natural
		  );
    port  (
        s_clk_i   : in std_logic;
        s_reset_n_i  : in std_logic;
        s_ce25_i            : in std_logic;
        s_column_count_i    : in unsigned(9 downto 0);
        s_line_count_i      : in unsigned(9 downto 0);
        s_blank_i           : in std_logic;
        s_image_finished_i  : in std_logic;
        s_red_o      : out std_logic_vector(G_RED_WIDTH-1 downto 0);
        s_green_o    : out std_logic_vector(G_GREEN_WIDTH-1 downto 0);
        s_blue_o     : out std_logic_vector(G_BLUE_WIDTH-1 downto 0)
    );
end entity spriteflyer_sprite;

architecture rtl of spriteflyer_sprite is

    signal sprite_x_min, sprite_x_max, sprite_y_min, sprite_y_max : unsigned(9 downto 0);

    constant SPRITE_HEIGHT : natural := 32;
    constant SPRITE_WIDTH  : natural := 32;
     
    signal s_sprite_line_address   : unsigned (4 downto 0); -- 0 to SPRITE_WIDTH-1
    signal s_sprite_line           : std_logic_vector(31 downto 0);
    signal s_sprite_data           : std_logic_vector(31 downto 0);

    signal s_sprite_oe : std_logic;

    signal s_x_state,s_y_state : std_logic;

    type t_sprite_rom is array (0 to SPRITE_HEIGHT-1) of std_logic_vector(SPRITE_WIDTH-1 downto 0);

--    signal s_sprite_rom : t_sprite_rom := (
--        "00000000000000000000000000000000",
--        "00000000000000000000000000000000",
--        "00000000000000000000000000000000",
--        "00000000000111111111100000000000",
--        "00000000011111111111111100000000",
--        "00000001111111111111111111000000",
--        "00000011111111111111111111100000",
--        "00000111111111111111111111110000",
--        "00001111111111111111111111111000",
--        "00001111111111111111111111111000",
--        "00011111111111111111111111111100",
--        "00011111111111111111111111111100",
--        "00011111111111111111111111111100",
--        "00011111111111111111111111111100",
--        "00011111111111111111111111111100",
--        "00011111111111111111111111111100",
--        "00001111111111111111111111111000",
--        "00001111111111111111111111111000",
--        "00000111111111111111111111110000",
--        "00000011111111111111111111100000",
--        "00000001111111111111111111000000",
--        "00000000111111111111111100000000",
--        "00000000001111111111111000000000",
--        "00000000000100001000010000000000",
--        "00000000000010001000100000000000",
--        "00000000000001001001000000000000",
--        "00000000000000111110000000000000",
--        "00000000000001111111000000000000",
--        "00000000000000111110000000000000",
--        "00000000000000111110000000000000",
--        "00000000000000000000000000000000",
--        "00000000000000000000000000000000"
--    );

--    signal s_sprite_rom : t_sprite_rom := (
--        "00000000000000000000000000000000",
--        "00000001100000000000000000000000",
--        "00000011110000000000000000000000",
--        "00000001111000000000000000000000",
--        "00000001111100000000000000000000",
--        "00000001111111110000000000000000",
--        "00000001111111110000000000000000",
--        "00000001111111100000000000000000",
--        "00000001111111100000000000000000",
--        "00110000111111110000000000000000",
--        "01110000111111111000000000000000",
--        "11111000111111111100000000000000",
--        "01111100111111111110000000000000",
--        "00111111111111111111010101000000",
--        "00111111111111111111111111111000",
--        "00111111111111111111111111111110",
--        "00111111111111111111111111111110",
--        "00111111111111111111111111111000",
--        "00111111111111111111011010000000",
--        "01111100111111111110000000000000",
--        "11111000111111111100000000000000",
--        "01111000111111111000000000000000",
--        "01100000111111110000000000000000",
--        "00000000111111100000000000000000",
--        "00000001111111100000000000000000",
--        "00000001111111110000000000000000",
--        "00000001111111110000000000000000",
--        "00000001111100000000000000000000",
--        "00000001111000000000000000000000",
--        "00000011110000000000000000000000",
--        "00000001100000000000000000000000",
--        "00000000000000000000000000000000"
--    );

    signal s_sprite_rom : t_sprite_rom := (
        "00000000000000000000000000000000",
        "00000011111000000000000000000000",
        "00000001111100000000000000000000",
        "00000001111110000000000000000000",
        "00000000111111000000000000000000",
        "00000000111111100000000000000000",
        "00000000011111100000000000000000",
        "00000000001111110000000000000000",
        "00000000001111111000000000000000",
        "11000000000111111100000000000000",
        "11100000000111111110000000000000",
        "11110000000011111111000000000000",
        "11111000000011111111100000000000",
        "11111111111111111111111111111110",
        "11111111111111111111111111111111",
        "01111111111111111111111111111111",
        "01111111111111111111111111111111",
        "11111111111111111111111111111111",
        "11111111111111111111111111111110",
        "11111000000011111111100000000000",
        "11110000000011111111000000000000",
        "11100000000111111110000000000000",
        "11000000000111111100000000000000",
        "00000000001111111000000000000000",
        "00000000001111110000000000000000",
        "00000000011111100000000000000000",
        "00000000111111100000000000000000",
        "00000000111111000000000000000000",
        "00000001111110000000000000000000",
        "00000001111100000000000000000000",
        "00000011111000000000000000000000",
        "00000000000000000000000000000000");

--    signal s_sprite_rom : t_sprite_rom := (
--        "00000000000000000000000000000111",
--        "00000000000000000000000000111111",
--        "00000000000000000000000001111110",
--        "00000000000000000000000111111110",
--        "00000000000000000000000111111110",
--        "00000000000000000000001111111110",
--        "00000000000000000000011111111100",
--        "00000000000000000000111111111000",
--        "00111111111111111111111111111000",
--        "01111111111111111111111111100000",
--        "01111111111111111111111111000000",
--        "00000111111111111111111110000000",
--        "00000011111111111111111100000000",
--        "00000000011111111111111100000000",
--        "00000000000111111111111100000000",
--        "00000000000001111111111110000000",
--        "00000000000111111111111100000000",
--        "00000000000111111111111100000000",
--        "00000000001111111111111100000000",
--        "00000000011111111111111100000000",
--        "00000101111111110011111100000000",
--        "00111111111111100011111100000000",
--        "00011111111110000011111100000000",
--        "00001111111100000001111100000000",
--        "00000111111100000001111100000000",
--        "00000111111100000001111100000000",
--        "00001111111100000000111100000000",
--        "00001110111000000000111100000000",
--        "00000000011000000000011100000000",
--        "00000000001000000000011100000000",
--        "00000000000000000000011100000000",
--        "00000000000000000000000000000000"
--    );


begin

    sprite_x_max <= sprite_x_min + SPRITE_WIDTH;
    sprite_y_max <= sprite_y_min + SPRITE_HEIGHT;

    -- generate sprite movement
    p_sprite_movement : process (s_clk_i, s_reset_n_i)
     begin
        if s_reset_n_i = '0' then
            sprite_x_min <= to_unsigned(C_HVISIBLE_START,sprite_x_min'length);
            sprite_y_min <= to_unsigned(C_VVISIBLE_START+C_VVISIBLE-SPRITE_HEIGHT,sprite_y_min'length);
            s_x_state      <= '0';
            s_y_state      <= '0';
        elsif s_clk_i'event and s_clk_i = '1' then
            if s_image_finished_i = '1' then
                if s_x_state = '1' then
                    if sprite_x_max < C_HVISIBLE_START+C_HVISIBLE then
                        sprite_x_min <= sprite_x_min + 1;
                    else
                        sprite_x_min <= sprite_x_min - 1;
                        s_x_state <= '0';
                    end if;
                else
                    if sprite_x_min > C_HVISIBLE_START-1 then
                        sprite_x_min <= sprite_x_min - 1;
                    else
                        s_x_state <= '1';
                        sprite_x_min <= sprite_x_min + 1;
                    end if;
                end if;
                if s_y_state = '1' then
                    if sprite_y_max < C_VVISIBLE_START+C_VVISIBLE then
                        sprite_y_min <= sprite_y_min + 1;
                    else
                        s_y_state <= '0';
                        sprite_y_min <= sprite_y_min - 1;
                    end if;
                else
                    if sprite_y_min > C_VVISIBLE_START-1 then
                        sprite_y_min <= sprite_y_min - 1;
                    else
                        s_y_state <= '1';
                        sprite_y_min <= sprite_y_min + 1;
                    end if;
                 end if;
            end if;
        end if;
    end process p_sprite_movement;

    p_sprite_rom : process (s_clk_i)
    begin
        if s_clk_i'event and s_clk_i = '1' then
            s_sprite_data <= s_sprite_rom(to_integer(s_sprite_line_address));
        end if;
    end process p_sprite_rom;

    p_sprite_comb : process (s_y_state,s_column_count_i,s_line_count_i,sprite_x_min,sprite_y_min,sprite_x_max,sprite_y_max)
        variable v_sprite_line_address : unsigned (9 downto 0);
    begin
        if (s_y_state = '0') then
            v_sprite_line_address := s_line_count_i - sprite_y_min;
        else
            v_sprite_line_address := SPRITE_HEIGHT - s_line_count_i + sprite_y_min - 1;
        end if;

        s_sprite_line_address <= v_sprite_line_address(4 downto 0);

        if s_column_count_i >= sprite_x_min and s_column_count_i < sprite_x_max and
           s_line_count_i   >= sprite_y_min and s_line_count_i   < sprite_y_max then
            s_sprite_oe <= '1';
        else
            s_sprite_oe <= '0';
        end if;
    end process p_sprite_comb;

    -- iterate sprite
    p_sprite : process (s_clk_i, s_reset_n_i)
    begin
        if s_reset_n_i = '0' then
            s_sprite_line   <= (others => '0');
        elsif s_clk_i'event and s_clk_i = '1' then
            if s_ce25_i = '1' then
                if s_column_count_i = sprite_x_min - 1 then
                    s_sprite_line <= s_sprite_data;
                else
                    if s_x_state = '0' then
                        s_sprite_line <= '0' & s_sprite_line(SPRITE_WIDTH-1 downto 1);
                    else
                        s_sprite_line <= s_sprite_line(SPRITE_WIDTH-2 downto 0) & '0';
                    end if;
                end if;
            end if;
        end if;
    end process p_sprite;

    p_rgb : process (s_clk_i, s_reset_n_i)
        variable v_sprite_idx : natural;
    begin
        if s_reset_n_i = '0' then
            s_red_o <= (others => '0');
            s_green_o <= (others => '0');
            s_blue_o  <= (others => '0');
        elsif s_clk_i'event and s_clk_i = '1' then
            v_sprite_idx := 0;
            if s_ce25_i = '1' then
                s_red_o <= (others => '0');
                s_green_o <= (others => '0');
                s_blue_o  <= (others => '0');
                if s_blank_i = '0' then
                    if s_x_state = '1' then
                        v_sprite_idx := SPRITE_WIDTH-1;
                    end if;
                    if s_sprite_oe = '0' or s_sprite_line(v_sprite_idx) = '0' then
                        -- background if sprite disabled or sprite pixel is background
                        s_red_o   <= not std_logic_vector(s_line_count_i(s_line_count_i'length-1 downto s_line_count_i'length-G_RED_WIDTH));
                        s_green_o <= std_logic_vector(s_line_count_i(s_line_count_i'length-1 downto s_line_count_i'length-G_GREEN_WIDTH));
                        s_blue_o  <= (others => '1');
                    end if;
                end if;
            end if;
        end if;
    end process p_rgb;

end rtl;