1 |
2 |
OmarMokhta |
library IEEE;
|
2 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
3 |
|
|
use IEEE.numeric_std.all;
|
4 |
|
|
use IEEE.std_logic_unsigned.all;
|
5 |
|
|
entity Bresenhamer is
|
6 |
|
|
Port ( WriteEnable : out STD_LOGIC;
|
7 |
|
|
X : out STD_LOGIC_VECTOR (9 downto 0);
|
8 |
|
|
Y : out STD_LOGIC_VECTOR (8 downto 0);
|
9 |
|
|
X1 : in STD_LOGIC_VECTOR (9 downto 0);
|
10 |
|
|
Y1 : in STD_LOGIC_VECTOR (8 downto 0);
|
11 |
|
|
X2 : in STD_LOGIC_VECTOR (9 downto 0);
|
12 |
|
|
Y2 : in STD_LOGIC_VECTOR (8 downto 0);
|
13 |
|
|
SS : out STD_LOGIC_VECTOR (3 downto 0);
|
14 |
|
|
Clk : in STD_LOGIC;
|
15 |
|
|
StartDraw : in STD_LOGIC;
|
16 |
|
|
dbg : out STD_LOGIC_VECTOR (11 downto 0);
|
17 |
|
|
Reset : in STD_LOGIC);
|
18 |
|
|
end Bresenhamer;
|
19 |
|
|
architecture Behavioral of Bresenhamer is
|
20 |
|
|
signal myX1,myX2 : STD_LOGIC_VECTOR (11 downto 0);
|
21 |
|
|
signal myY1,myY2 : STD_LOGIC_VECTOR (11 downto 0);
|
22 |
|
|
signal p,p0_1,p0_2,p0_3,p0_4,p0_5,p0_6,p0_7,p0_8 : STD_LOGIC_VECTOR (11 downto 0);
|
23 |
|
|
signal p_1,p_2,p_3,p_4,p_5,p_6,p_7,p_8 : STD_LOGIC_VECTOR (11 downto 0);
|
24 |
|
|
signal ndx,ndy : STD_LOGIC_VECTOR (11 downto 0);
|
25 |
|
|
signal dx,dy,t_2dx,t_2dy,neg_dx,neg_dy,t_2neg_dx,t_2neg_dy : STD_LOGIC_VECTOR (11 downto 0);
|
26 |
|
|
signal dx_minus_dy : STD_LOGIC_VECTOR (11 downto 0);
|
27 |
|
|
signal minus_dx_minus_dy : STD_LOGIC_VECTOR (11 downto 0);
|
28 |
|
|
signal minus_dx_plus_dy : STD_LOGIC_VECTOR (11 downto 0);
|
29 |
|
|
signal dx_plus_dy : STD_LOGIC_VECTOR (11 downto 0);
|
30 |
|
|
signal State : STD_LOGIC_VECTOR(3 downto 0) := "0000";
|
31 |
|
|
signal condX1X2,condY1Y2 : STD_LOGIC;
|
32 |
|
|
constant IDLE : STD_LOGIC_VECTOR(3 downto 0) := "0000";
|
33 |
|
|
constant INIT : STD_LOGIC_VECTOR(3 downto 0) := "0001";
|
34 |
|
|
constant CASE1 : STD_LOGIC_VECTOR(3 downto 0) := "0010";
|
35 |
|
|
constant CASE2 : STD_LOGIC_VECTOR(3 downto 0) := "0011";
|
36 |
|
|
constant CASE3 : STD_LOGIC_VECTOR(3 downto 0) := "0100";
|
37 |
|
|
constant CASE4 : STD_LOGIC_VECTOR(3 downto 0) := "0101";
|
38 |
|
|
constant CASE5 : STD_LOGIC_VECTOR(3 downto 0) := "0110";
|
39 |
|
|
constant CASE6 : STD_LOGIC_VECTOR(3 downto 0) := "0111";
|
40 |
|
|
constant CASE7 : STD_LOGIC_VECTOR(3 downto 0) := "1000";
|
41 |
|
|
constant CASE8 : STD_LOGIC_VECTOR(3 downto 0) := "1001";
|
42 |
|
|
constant CLEAR : STD_LOGIC_VECTOR(3 downto 0) := "1010";
|
43 |
|
|
signal ccounter : STD_LOGIC_VECTOR (18 downto 0) := "0000000000000000000";
|
44 |
|
|
begin
|
45 |
|
|
ndx <= ("00" & X2)-("00" & X1);
|
46 |
|
|
ndy <= ("000" & Y2)-("000" & Y1);
|
47 |
|
|
neg_dx <= 0-dx;
|
48 |
|
|
neg_dy <= 0-dy;
|
49 |
|
|
dbg <= p;
|
50 |
|
|
dx_minus_dy <= dx+neg_dy;
|
51 |
|
|
minus_dx_minus_dy <= neg_dx+neg_dy;
|
52 |
|
|
minus_dx_plus_dy <= neg_dx+dy;
|
53 |
|
|
dx_plus_dy <= dx+dy;
|
54 |
|
|
t_2dy <= dy(10 downto 0) & '0';
|
55 |
|
|
t_2dx <= dx(10 downto 0) & '0';
|
56 |
|
|
t_2neg_dy <= neg_dy(10 downto 0) & '0';
|
57 |
|
|
t_2neg_dx <= neg_dx(10 downto 0) & '0';
|
58 |
|
|
p0_1 <= t_2dy+neg_dx;
|
59 |
|
|
p0_2 <= t_2dx+neg_dy;
|
60 |
|
|
p0_3 <= t_2neg_dx+dy;
|
61 |
|
|
p0_4 <= t_2dy+neg_dx;
|
62 |
|
|
p0_5 <= t_2neg_dy+dx;
|
63 |
|
|
p0_6 <= t_2neg_dx+dy;
|
64 |
|
|
p0_7 <= t_2dx+neg_dy;
|
65 |
|
|
p0_8 <= t_2neg_dy+dx;
|
66 |
|
|
p_1 <= p+t_2dy when p(11)='1' else p+t_2dy+t_2neg_dx;
|
67 |
|
|
p_2 <= p+t_2dx when p(11)='1' else p+t_2dx+t_2neg_dy;
|
68 |
|
|
p_3 <= p+t_2neg_dx when p(11)='1' else p+t_2neg_dx+t_2neg_dy;
|
69 |
|
|
p_4 <= p+t_2dy when p(11)='1' else p+t_2dy+t_2dx;
|
70 |
|
|
p_5 <= p+t_2neg_dy when p(11)='1' else p+t_2neg_dy+t_2dx;
|
71 |
|
|
p_6 <= p+t_2neg_dx when p(11)='1' else p+t_2neg_dx+t_2dy;
|
72 |
|
|
p_7 <= p+t_2dx when p(11)='1' else p+t_2dx+t_2dy;
|
73 |
|
|
p_8 <= p+t_2neg_dy when p(11)='1' else p+t_2neg_dy+t_2neg_dx;
|
74 |
|
|
X <= ccounter(9 downto 0) when State = CLEAR else myX1(9 downto 0);
|
75 |
|
|
Y <= ccounter(18 downto 10) when State = CLEAR else myY1(8 downto 0);
|
76 |
|
|
SS <= State;
|
77 |
|
|
WriteEnable <= '0' when State = IDLE or State = INIT else '1';
|
78 |
|
|
process (Clk) begin
|
79 |
|
|
if (rising_edge(Clk)) then
|
80 |
|
|
if (State = IDLE) then
|
81 |
|
|
if (Reset = '1') then
|
82 |
|
|
State <= CLEAR;
|
83 |
|
|
ccounter <= (others=>'0');
|
84 |
|
|
elsif (StartDraw = '1') then
|
85 |
|
|
myX1(9 downto 0) <= X1;
|
86 |
|
|
myX1(11 downto 10) <= "00";
|
87 |
|
|
myY1(8 downto 0) <= Y1;
|
88 |
|
|
myY1(11 downto 9) <= "000";
|
89 |
|
|
myX2(9 downto 0) <= X2;
|
90 |
|
|
myX2(11 downto 10) <= "00";
|
91 |
|
|
myY2(8 downto 0) <= Y2;
|
92 |
|
|
myY2(11 downto 9) <= "000";
|
93 |
|
|
dx <= ndx;
|
94 |
|
|
dy <= ndy;
|
95 |
|
|
State <= INIT;
|
96 |
|
|
end if;
|
97 |
|
|
elsif (State = INIT) then
|
98 |
|
|
if (dx(11) = '0' and dy(11) = '0' and dx_minus_dy(11) = '0') then
|
99 |
|
|
State <= CASE1;
|
100 |
|
|
p <= p0_1;
|
101 |
|
|
elsif (dx(11) = '0' and dy(11) = '0' and dx_minus_dy(11) = '1') then
|
102 |
|
|
State <= CASE2;
|
103 |
|
|
p <= p0_2;
|
104 |
|
|
elsif (dx(11) = '1' and dy(11) = '0' and minus_dx_minus_dy(11) = '1') then
|
105 |
|
|
State <= CASE3;
|
106 |
|
|
p <= p0_3;
|
107 |
|
|
elsif (dx(11) = '1' and dy(11) = '0' and minus_dx_minus_dy(11) = '0') then
|
108 |
|
|
State <= CASE4;
|
109 |
|
|
p <= p0_4;
|
110 |
|
|
elsif (dx(11) = '1' and dy(11) = '1' and minus_dx_plus_dy(11) = '0') then
|
111 |
|
|
State <= CASE5;
|
112 |
|
|
p <= p0_5;
|
113 |
|
|
elsif (dx(11) = '1' and dy(11) = '1' and minus_dx_plus_dy(11) = '1') then
|
114 |
|
|
State <= CASE6;
|
115 |
|
|
p <= p0_6;
|
116 |
|
|
elsif (dx(11) = '0' and dy(11) = '1' and dx_plus_dy(11) = '1') then
|
117 |
|
|
State <= CASE7;
|
118 |
|
|
p <= p0_7;
|
119 |
|
|
else
|
120 |
|
|
State <= CASE8;
|
121 |
|
|
p <= p0_8;
|
122 |
|
|
end if;
|
123 |
|
|
elsif (State = CASE1) then
|
124 |
|
|
if (myX1 = myX2) then
|
125 |
|
|
State <= IDLE;
|
126 |
|
|
else
|
127 |
|
|
myX1 <= myX1 + 1;
|
128 |
|
|
p <= p_1;
|
129 |
|
|
if (P(11) = '0') then
|
130 |
|
|
myY1 <= myY1 + 1;
|
131 |
|
|
end if;
|
132 |
|
|
end if;
|
133 |
|
|
elsif (State = CASE2) then
|
134 |
|
|
if (myY1 = myY2) then
|
135 |
|
|
State <= IDLE;
|
136 |
|
|
else
|
137 |
|
|
myY1 <= myY1 + 1;
|
138 |
|
|
p <= p_2;
|
139 |
|
|
if (P(11) = '0') then
|
140 |
|
|
myX1 <= myX1 + 1;
|
141 |
|
|
end if;
|
142 |
|
|
end if;
|
143 |
|
|
elsif (State = CASE3) then
|
144 |
|
|
if (myY1 = myY2) then
|
145 |
|
|
State <= IDLE;
|
146 |
|
|
else
|
147 |
|
|
myY1 <= myY1 + 1;
|
148 |
|
|
p <= p_3;
|
149 |
|
|
if (P(11) = '0') then
|
150 |
|
|
myX1 <= myX1 - 1;
|
151 |
|
|
end if;
|
152 |
|
|
end if;
|
153 |
|
|
elsif (State = CASE4) then
|
154 |
|
|
if (myX1 = myX2) then
|
155 |
|
|
State <= IDLE;
|
156 |
|
|
else
|
157 |
|
|
myX1 <= myX1 - 1;
|
158 |
|
|
p <= p_4;
|
159 |
|
|
if (P(11) = '0') then
|
160 |
|
|
myY1 <= myY1 + 1;
|
161 |
|
|
end if;
|
162 |
|
|
end if;
|
163 |
|
|
elsif (State = CASE5) then
|
164 |
|
|
if (myX1 = myX2) then
|
165 |
|
|
State <= IDLE;
|
166 |
|
|
else
|
167 |
|
|
myX1 <= myX1 - 1;
|
168 |
|
|
p <= p_5;
|
169 |
|
|
if (P(11) = '0') then
|
170 |
|
|
myY1 <= myY1 - 1;
|
171 |
|
|
end if;
|
172 |
|
|
end if;
|
173 |
|
|
elsif (State = CASE6) then
|
174 |
|
|
if (myY1 = myY2) then
|
175 |
|
|
State <= IDLE;
|
176 |
|
|
else
|
177 |
|
|
myY1 <= myY1 - 1;
|
178 |
|
|
p <= p_6;
|
179 |
|
|
if (P(11) = '0') then
|
180 |
|
|
myX1 <= myX1 - 1;
|
181 |
|
|
end if;
|
182 |
|
|
end if;
|
183 |
|
|
elsif (State = CASE7) then
|
184 |
|
|
if (myY1 = myY2) then
|
185 |
|
|
State <= IDLE;
|
186 |
|
|
else
|
187 |
|
|
myY1 <= myY1 - 1;
|
188 |
|
|
p <= p_7;
|
189 |
|
|
if (P(11) = '0') then
|
190 |
|
|
myX1 <= myX1 + 1;
|
191 |
|
|
end if;
|
192 |
|
|
end if;
|
193 |
|
|
elsif (State = CASE8) then
|
194 |
|
|
if (myX1 = myX2) then
|
195 |
|
|
State <= IDLE;
|
196 |
|
|
else
|
197 |
|
|
myX1 <= myX1 + 1;
|
198 |
|
|
p <= p_8;
|
199 |
|
|
if (P(11) = '0') then
|
200 |
|
|
myY1 <= myY1 - 1;
|
201 |
|
|
end if;
|
202 |
|
|
end if;
|
203 |
|
|
elsif (State = CLEAR) then
|
204 |
|
|
ccounter <= ccounter + 1;
|
205 |
|
|
if (ccounter = "1111111111111111111") then
|
206 |
|
|
State <= IDLE;
|
207 |
|
|
end if;
|
208 |
|
|
end if;
|
209 |
|
|
end if;
|
210 |
|
|
end process;
|
211 |
|
|
end Behavioral;
|