本帖最后由 cll 于 2010-6-18 21:04 编辑
nir说的很对,理解的理解,不理解还是不理解,还有不是流人的流人,见我的贴
万源归宗:单片机与FPGA演义第一回
本人是一名电子爱好者,93年用51 2000年学用FPGA及ARM到现在,在这里聊一些经验感想,供大伙解闷.
话说武术最高境界--万源归宗,当代也有一位电子宗师,他用74LS系列做了一台计算机,带MMU,移植了GCC,配操作系统,看官想知道他是谁呢,后文中会一一解说,以此作为开头,单片机与FPGA万源归宗。
第一式:有招胜无招
Bresenham画线算法 单片机程序如下:
int BresenhamLine ( int x1 , int y1 , int x2 , int y2 , int c)
{
int dx , dy ;
int tx , ty ;
int inc1 , inc2 ;
int d , iTag ;
int x , y ;
putpixel ( x1 , y1 , c ) ;
if ( x1 == x2 && y1 == y2 )
return 1 ;
iTag = 0 ;
dx = abs ( x2 - x1 );
dy = abs ( y2 - y1 );
if ( dx < dy )
{
iTag = 1 ;
Swap ( & x1 , & y1 );
Swap ( & x2 , & y2 );
Swap ( & dx , & dy );
}
tx = ( x2 - x1 ) > 0 ? 1 : -1 ;
ty = ( y2 - y1 ) > 0 ? 1 : -1 ;
x = x1 ;
y = y1 ;
inc1 = 2 * dy ;
inc2 = 2 * ( dy - dx );
d = inc1 - dx ;
while ( x != x2 )
{
if ( d < 0 )
d += inc1 ;
else
{
y += ty ;
d += inc2 ;
}
if ( iTag )
putpixel ( y , x , c ) ;
else
putpixel ( x , y , c ) ;
x += tx ;
}
return 0;
}
Swap ( int * a , int * b )
{
int tmp ;
tmp = * a ;
* a = * b ;
* b = tmp ;
}
FPGA实现如下verilog HDL :
module line
(
input[31 :0] page_address,
input clk_i,
input rstn_i,
input load_i,
input ack_i,
input signed [15:0] sx,
input signed [15:0] sy,
input signed [15:0] ex,
input signed [15:0] ey,
input[23:0] f_color_i,
input[23:0] b_color_i,
input[7 :0] data_i,
output[23 :0] data_o,
output[23 :0] addr_o,
output pset_start_o,
output reg line_over_o
);
reg[23: 0] addr;
reg[7 :0] rdata_i;
reg signed [15:0] x;
reg signed [15:0] y;
reg signed [15 :0] xsign;
reg signed [15 :0] ysign;
reg signed [16 :0] delta_x;
reg signed [16 :0] delta_y;
reg [16 :0] rdelta_x;
reg [16 :0] rdelta_y;
reg signed [16 :0] i;
reg signed [16 :0] e;
reg change;
reg [3:0]state;
reg pset_load;
wire over_o;
// Declare states
parameter S0 = 0, S1 = 1, S2 = 2, S3 = 3,S4 = 4, S5 = 5,S6 =6, S7=7,S40 = 8;
// Determine the next state synchronously, based on the
// current state and the input
always @ (posedge clk_i or negedge rstn_i) begin
if (! rstn_i)
begin
line_over_o <= 1'b0;
rdata_i <= 8'd0;
addr <= 24'd0;
xsign <= 16'd0;
ysign <= 16'd0;
delta_x <= 17'd0;
delta_y <= 17'd0;
i <= 17'd0;
e <= 17'd0;
pset_load <= 1'b0;
state <= S0;
end
else
case (state)
S0:
if(load_i)
begin
line_over_o <= 1'b1;
x <= sx;
y <= sy;
delta_x <= ex - sx;
delta_y <= ey - sy;
state <= S1;
end
else
begin
line_over_o <= 1'b0;
state <= S0;
end
S1:
begin
if(delta_x < 0 ) begin rdelta_x <= (~ delta_x )+ 1'b1; xsign<= -1; end
else begin rdelta_x <= delta_x ; xsign <= 1; end
if(delta_y < 0 ) begin rdelta_y <= (~ delta_y )+ 1'b1; ysign <= -1; end
else begin rdelta_y <= delta_y ; ysign <= 1; end
state <= S2;
end
S2:
begin
if(rdelta_x < rdelta_y)
begin
delta_x <= rdelta_y;
delta_y <= rdelta_x;
change <= 1'b1;
end
else
begin
delta_x <= rdelta_x;
delta_y <= rdelta_y;
change <= 1'b0;
end
state <= S3;
end
S3:
begin
e <= ( delta_y * 2 ) - delta_x;
i <= 17'd1;
rdata_i <= data_i;
state <= S4;
end
S4:
begin
addr <=( ( y * 1024 ) + x ) + page_address[23 :0];
pset_load <= 1'b1;
state <= S40;
end
S40:
begin
if(over_o == 1'b1 )
begin
pset_load <= 1'b0;
state <= S5;
end
else
state <= S40;
end
S5:
begin
if( e >= 0 )
begin
if(change == 1'b1) x <= x + xsign;
else y <= y + ysign;
e <= e + ( delta_x * 2);
state <= S5;
end
else
state <= S6;
end
S6:
begin
if( change == 1'b1) y <= y + ysign;
else x <= x + xsign;
e <= e + ( delta_y * 2);
state <= S7;
end
S7:
begin
if(i < delta_x )
begin
i <= i + 1'b1;
state <= S4;
end
else
begin
line_over_o <= 1'b0;
state <= S0;
end
end
endcase
end
endmodule
verilog程序是我做VGA卡的其中的模块.
心中有招,刀剑棍棒皆可用,无论单片机,FPGA或是74LS,本回就此停住。
单片机,dsp ,cpu,都是有限状态机的特例,就数字电路来讲,只是其中的一部分,由此而来,统称的 类mcu,cpu等
都有其特定的长处与短处,软件的东西可以硬件做,硬件的东西可以软件模拟,南拳北腿,内外兼修,方能登堂入室
具体的列子 在第二式:内外兼修 分解。 |