Verilog實現Bresenham任意斜率直線

2021-10-09 12:00:13 字數 3024 閱讀 7940

根據bresenham演算法原理:

簡單修改後支援任意斜率直線:

reg [15:0] dx;

reg [15:0] dy;

reg signed [15:0] ix;

reg signed [15:0] iy;

reg signed [15:0] iw;

always @(posedge clk)

begin

if (m_req == 1'b1) begin

dx <= ($unsigned(addr_x1) > $unsigned(addr_x0))?($unsigned(addr_x1) - $unsigned(addr_x0)):($unsigned(addr_x0) - $unsigned(addr_x1));//求絕對值

dy <= ($unsigned(addr_y1) > $unsigned(addr_y0))?($unsigned(addr_y1) - $unsigned(addr_y0)):($unsigned(addr_y0) - $unsigned(addr_y1));//求絕對值

ix <= ($unsigned(addr_x1) > $unsigned(addr_x0))?1:-1;//x軸步進

iy <= ($unsigned(addr_y1) > $unsigned(addr_y0))?1:-1;//y軸步進

iw <= ($unsigned(addr_y1) > $unsigned(addr_y0))?1280:-1280; //影象為1280*720

endendreg signed [15:0] n2dy;

reg signed [15:0] n2dydx;

reg signed [15:0] n2dx;

reg signed [15:0] n2dxdy;

reg signed [15:0] stepx;

reg signed [15:0] stepy;

always @(posedge clk)

begin

if (m_req_d1 == 1'b1) begin

n2dy <= (dy << 1);

n2dydx <= (dy - dx) << 1;

n2dx <= (dx << 1);

n2dxdy <= (dx - dy) << 1;

endendalways @(posedge clk)

begin

if (m_req_d1 == 1'b1) begin

stepy <= (dy << 1) - dx;

end else if (addr_wr) begin

if(cx != addr_x1 || cy != addr_y1)begin

if(dx>dy)begin

if(stepy > 0)

stepy <= stepy + n2dydx;

else

stepy <= stepy + n2dy;

end//dy>dx, stepy 常》0;

endend

endalways @(posedge clk)

begin

if (m_req_d1 == 1'b1) begin

stepx <= (dx << 1) - dy;

end else if (addr_wr) begin

if(cx != addr_x1 || cy != addr_y1)begin

if(dy>dx)begin

if(stepx > 0)

stepx <= stepx + n2dxdy;

else

stepx <= stepx + n2dx;

end//dx>dy, stepx 常》0;

endend

endreg [15:0] cx;

reg [15:0] cy;

always @(posedge clk)

begin

if (rst_n == 0 || m_req == 1'b1) begin

addr <= $unsigned(addr0[15:0])*1280 + $unsigned(addr0[31:16]);

cx <= addr0[31:16];

cy <= addr0[15:0];

end else if (addr_wr) begin

if(cx != addr_x1 || cy != addr_y1)begin

if(stepx > 0)//x軸誤差累計

cx <= cx + ix;

if(stepy > 0)//y軸誤差累計

cy <= cy + iy;

addr <= addr + ((stepx>0)?ix:0) + ((stepy>0)?iw:0);

endend

end

斜率》-1: p0(300, 300)~p1(100,200);

斜率

斜率<1: p0(100,200)~p1(300,300)

斜率》1: p0(200,100)~p1(300,300)

垂直: p0(300,100)~p1(300,300)

水平: p0(100,300)~p1(300,300)

verilog實現串列埠

實現串列埠的時候一些簡單的介紹和遇到問題和思考。備忘 實現時可以把這兩部分分開。也就是兩個 fifo 配套收和發的 v檔案,可以實現乙個串列埠。一般人實現串列埠的都是這麼想的吧。下面是檔案的層級結構 2 當然是coding 了。寫串列埠的 主要貼一下收和發的狀態機 用一段來實現的 1 這是收的部分 ...

verilog簡單實現串列埠

uart 2017.10.7 傳送接收到的資料 波特率9600 乙個停止位 無奇偶校驗 de2板子 rx gpio k25 0 left tx tx gpio k26 1 right rx gnd 右邊第6 或者倒數第6 復位不成功 復位按鍵有問題 靠右按即可復位 特別注意 如果乙個條件中同時給乙個...

卷積編碼verilog實現

完成 2,1,7 編碼方式 多項式為 133,171 也可以完成 2,1,5 編碼方式多項式 23,35 的verilog實現 對應 中的注釋的位置。相應的解碼器也寫已寫完,後續。相關的卷積編碼的基礎知識請參看 timescale 1ns 1ps company engineer create da...