line
小君童靴说头儿给了他一个project,实现给出屏幕上任意两个点,求出这两个点之间直线上的所有的点。觉得这个很好玩,就自己也写了一点code
/*
date : 2014/10/21
version : QuartusII 14.0 + DE1-SOC(cycloneV)
function: 输入两个点 Xmax = 1023 Ymax = 511 ,计算出中间点的坐标 说明 : (1)1023=0x3ff (10bit) 511 = 0x1ff (9bit)
(2)直线方程: y=kx+m ;
(3)第116行 装载Ymin值的时候-1, 要求输入Ymin不可以小于1:如
果这个地方不剪掉1,会导致输出有(Ymax-Ymin)个脉冲的延时
(4)现象三的原因是 144 行的判定方法导致。判定当前点为合适点的
方式是第二次离直线最近,第一次prev2_dm 较大,第三次curr_dm也较大,116
行不减1就无法满足“远,近,远”的判定,从而一直扫到最后都找不到合适点。
(5)本程序的计算方式是最直接的运算,计算量大,浪费硬件资源,更
好的算法等待大家的指点
(6)由于计算输出延时了三个周期,如150行,所以最后三个点无法覆盖到。
*/ module line (
clock ,
reset ,
xs_in , //输入的X 点的起始坐标
ys_in , // 输入的Y 点的起始坐标
xe_in , //输入X点的终止坐标
ye_in , //输入Y 点的终止坐标
in_en , //当前输入信号有效标志 1:有效 0:无效 x_ou, //输出的X 点的坐标
y_ou, // 输出的Y 点的坐标
fini_flag //计算完成标志位
);
input clock ,reset ;
input in_en ;
input [:] xs_in ,xe_in ;
input [:] ys_in ,ye_in ; output reg [:] x_ou ;
output reg [:] y_ou ;
output reg fini_flag ; wire signed [:] dx ; // X方向上的变化量
wire signed [:] dy ; //Y方向上的变化量
reg signed [:] line_k ; // 用来存储线条的斜率
reg signed [:] line_m ; // 直线的常数项
reg signed [:] curr_m ; //当前m值
reg [:] curr_dm ; //当前的m偏差量
reg [:] prev1_dm ; //上一次的m偏差量
reg [:] prev2_dm ; //上上一次m的偏差量
reg point_flag ; //找到当前最近点的标志位 wire signed [:] Xmin ;
wire signed [:] Xmax ;
wire signed [:] Ymin ;
wire signed [:] Ymax ; assign dx = xe_in-xs_in; //得出X方向上的差值
assign dy = ye_in-ys_in; //得出Y方向上的差值 //求得所需的直线就在这么一个矩形框内,针对这个矩形框进行运算
assign Xmin = (xs_in<xe_in)? xs_in : xe_in ;
assign Xmax = (xs_in<xe_in)? xe_in : xs_in ;
assign Ymin = (ys_in<ye_in)? ys_in : ye_in ;
assign Ymax = (ys_in<ye_in)? ye_in : ys_in ; //绝对值函数
function [:] abs (input signed[:] data1, input signed [:] data2 );
abs = (data1>data2) ? (data1-data2):(data2-data1);
endfunction //*********斜率的计算,并且扩大了2^6次方倍,并得出M值的2^6倍的大小******************
always @ (posedge clock)
if(!in_en)
begin
//line_k <= 16'd0 ;
//line_m <= 16'd0 ;
end
else
begin
line_k <= (dy<<)/(dx) ;
line_m <= (ye_in<<) - line_k*xe_in ;
end reg [:] x_cnt ; // X 坐标计数
reg [:] y_cnt ; // Y 坐标计数
//*********************矩形框X方向上计数*********************************
always @ (posedge clock )
if(!reset)
begin
x_cnt <= 'd0 ;
fini_flag <= 'd0 ;
end
else if (in_en) //装载Xmin值
begin
x_cnt <= Xmin + 'd1 ;
fini_flag <= 'd0 ;
end
else if (x_cnt == Xmax) //矩形框扫描完毕
begin
fini_flag <= 'd1 ;
end
else if ((y_cnt==Ymax)||(point_flag)) //列扫描完毕,x+1
begin
x_cnt <= x_cnt + 'd1 ;
fini_flag <= 'd0 ;
end //********************矩形框Y方向上计数 **************
always @ (posedge clock )
if(!reset)
begin
y_cnt <= 'd0 ;
end
else if (in_en) //装载Ymin值
begin
y_cnt <= Ymin - 'd1 ;
end
else if ((y_cnt == Ymax)||(point_flag)) //列扫描完毕重新装载运算
begin
y_cnt <= Ymin ;
end
else begin
y_cnt <= y_cnt + 'd1 ;
end always @ (posedge clock )
if ((!reset) || (in_en))
begin
x_ou <= 'd0 ;
y_ou <= 'd0 ;
point_flag <= 'd0 ;
prev1_dm <= 'd0 ;
prev2_dm <= 'd0 ;
curr_dm <= 'd0 ;
end
else if (!fini_flag)
begin
//point_flag <= 1'd0 ;
curr_m <= (y_cnt<<)-(x_cnt*line_k) ;
prev1_dm <= curr_dm ;
prev2_dm <= prev1_dm;
curr_dm <= abs(curr_m , line_m) ;
if((prev1_dm<curr_dm) &&(prev1_dm < prev2_dm)) //当前点在远离直线,以上一次的点有效
begin
point_flag <= 'd1 ; //找到最近点,x,y跳转,结束x列的查找
prev1_dm <= 'h00 ;
curr_dm <= 'h00 ;
prev2_dm <= 'h00 ;
x_ou <= x_cnt;
y_ou <= y_cnt - 'd3 ;
end
else point_flag <= 'd0 ;
end
else begin
point_flag <= 'd0 ;
prev1_dm <= 'h00 ;
curr_dm <= 'h00 ;
prev2_dm <= 'h00 ;
x_ou <= 'd0 ;
y_ou <= 'd0 ;
end endmodule
附上测试tb
`timescale 1ns/1ps module line_tb ; reg clock ,reset ;
reg in_en ;
reg [:] xs_in ,xe_in ;
reg [:] ys_in ,ye_in ; wire [:] x_ou ;
wire [:] y_ou ;
wire fini_flag ; line U1_line(
.clock (clock),
.reset (reset),
.xs_in (xs_in),
.ys_in (ys_in),
.xe_in (xe_in),
.ye_in (ye_in),
.in_en (in_en), .x_ou (x_ou),
.y_ou (y_ou),
.fini_flag (fini_flag)
); always # clock = ~clock ; initial
begin
clock = 'd0 ; reset =1'd0 ; in_en = 'd0 ;
xs_in = 'd0 ; xe_in = 10'd0 ;
ys_in = 'd0 ; ye_in = 9'd0 ; # reset = ; in_en = ;
xs_in = ; xe_in = ;
ys_in = ; ye_in = ;
# in_en = ;
# ;
# ;
in_en = ;
xs_in = ; xe_in= ;
ys_in = ; ye_in= ;
# in_en = ;
# $stop ; end endmodule
其实我听到这个project第一反应是软件中的两个for循环查找差值最小的点,再对应到HDL中想到的是generate,generate写到一半才发现输出怎么办,难道先让结果放到memory里面再慢慢取出来?觉得这么做是不是太繁琐了,于是叉掉重现写。
第二次想到的是用if else 实现软件中的for,写了n久都觉得这个逻辑关系很纠结,if else 都嵌套了。再加一层嵌套就不利于维护了,证明这个架构是不行的。
最后一想不是可以用计数器实现for吗,哎,是不是最近一直在看C,怎么把HDL设计思想和C 想混合了。对于大师来说我的这个思维历程很搞笑,对于菜鸟的我来说是第一次亲身感受到了软硬件代码设计思想的差异—— 以前只是书上说自己没有感触到
line的更多相关文章
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.
启动tomcat, 出现, ( 之前都是好好的... ) [lk ] ERROR [08-12 15:10:02] [main] org.springframework.web.context.Con ...
- 关于xml加载提示: Error on line 1 of document : 前言中不允许有内容
我是在java中做的相关测试, 首先粘贴下报错: 读取xml配置文件:xmls\property.xml org.dom4j.DocumentException: Error on line 1 of ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- Linix登录报"/etc/profile: line 11: syntax error near unexpected token `$'{\r''"
同事反馈他在一测试服务器(CentOS Linux release 7.2.1511)上修改了/etc/profile文件后,使用source命令不能生效,让我帮忙看看,结果使用SecureCRT一登 ...
- [LeetCode] Line Reflection 直线对称
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- [LeetCode] Tenth Line 第十行
How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...
- [LeetCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- "Installation failed !" in GUI but not in CLI (/usr/bin/winusb: line 78: 18265 Terminated )
"Installation failed !" in GUI but not in CLI (/usr/bin/winusb: line 78: 18265 Terminated ...
- How to build .apk file from command line(转)
How to build .apk file from command line Created on Wednesday, 29 June 2011 14:32 If you don’t want ...
随机推荐
- ASP.NET 使用My97DatePicker日期控件
首先要下载该控件的包,下载地址:http://pan.baidu.com/s/1Aa5gk 引用文件 <script src="js/My97DatePicker/WdatePicke ...
- 转: sublime text常用插件和快捷键
Sublime Text 2是一个轻量.简洁.高效.跨平台的编辑器.博主之前一直用notepdd++写前端代码,用得也挺顺手了,早就听说sublime的大名,一直也懒得去试试看,认为都是工具用着顺手就 ...
- hdu4336 Card Collector 状态压缩dp
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Ext JS学习第七天 Ext自定义类,继承(二)
此文来记录学习笔记 一个简单ext继承的栗子 Ext.onReady(function () { Ext.define('Person',{ config:{ name:'z3' } , constr ...
- Android滑动事件冲突
首先,我们假设这样一个场景:一个ViewPager里面嵌套一个ViewPager,内部滑动方向和外部滑动方向一样时,该怎么解决这一冲突呢? 针对滑动冲突这里给出两种解决方案:外部拦截法,内部拦截法. ...
- [译]SSRS 编写带参数的MDX报表
编写MDX报表长久以来对于报表人员来说都比较痛苦. 当然如果你用查询设计器(Query Designer) 直接拖拉数据集那就很方便,但是你们有没有想过查询设计器是怎么创建MDX的.或者创建的参数是如 ...
- HDU 1003(A - 最大子段和)
HDU 1003(A - 最大子段和) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/A 题目: ...
- openstack第1天
入门openstack题外篇 老实说,我在写这篇文章的时候,对云的了解还是比较模糊的,也许是刚接触,不管怎样 总得写点什么,写完之后也许数月之后,感觉写的不是那么好,到时候在做修该吧! 今天我们就提一 ...
- django 基础入门(一)
1. django 基本命令 新建project django-admin.py startproject project-name 新建app python manage.py startapp a ...
- nginx启动
查看nginx的进程 ps -ef | grep nginx 重启nginx的3种办法1.service nginx restart2.改了配置文件让其生效办法 nginx -s reload3.到n ...