Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20750 Discussions

Help with verilog code, calculator

Altera_Forum
Honored Contributor II
2,026 Views

Hello, i don't know if this is the correct place to put this post, if not, please move it! 

 

I'm a newbie when it comes to programming in Verilog, I'm currently working on a final project for my class in school. I'm doing a calculator. This is not an advanced calculator, and its solved in a different way than what i think you normally do. Anyway, I'm using the DE2 board, and my problem is concerning the division. 

 

always@(negedge key3) begin div_summa1=((siffra1*10)+siffra2); div_summa2=((siffra3*10)+siffra4); if(div_summa1<1 | div_summa2<1) begin div_ental=0; div_tiotal=0; end else if(div_summa1<10 & div_summa1>0 & div_summa2<10 & div_summa2>0) div_ental=(div_summa1/div_summa2); else begin if((div_summa1/div_summa2)<10) div_ental=div_summa1/div_summa2; else begin div_tiotal=(div_summa1/div_summa2)/10; div_ental=(div_summa1/div_summa2)-(((div_summa1/div_summa2)/10)*10); div_summa3=((((siffra1*10)+siffra2)/((siffra3*10)+siffra4))-((div_tiotal*10)+div_ental))*10+1; end end endI think it might be hard to understand from this part of the code without much of an explanation but maybe you don't have to understand.  

 

I'm letting the user enter each numbers with 4 switches binary. And i convert it directly. So if you wanna enter 45... you enter a 4 binary first, which then directly gets converted to 4 decimal, and then i take it times 10 in the program and so on...  

 

I'm only using two digit numbers so they can never be bigger than 99. Anyway, the problem I'm having is that, when for example I want to take 45/2, I should get 22,5 ... but right now I'm only using integers. I'm simply wondering if there is ANY variable or whatsoever that can handle the decimals? Im trying to in some way pick it out and take 0.5*10 to get it into 5 which i can display later on as a decimal but i can't do that as long as i work with integers. 

 

Just ask if you wanna see the whole code, I've used Swedish names for the variables so it might be hard to understand, that's why i didn't post the whole code here. 

 

Thanks for your help! 

//Reckan
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
707 Views

Hi Reckan, 

Welcome to Altera forum. 

It's difficult to handle floating point numbers, so I think you can use the Altera's IP mega function lpm_divide (http://quartushelp.altera.com/9.1/master.htm#mergedprojects/hdl/mega/mega_file_lpm_divide.htm?gsa_pos=1&wt.oss_r=1&wt.oss=lpm_divide). You can take the remaining part and use it with integer operations. You can simply multiply the number by 10 or 100 (depending on how many digits you want after the whole part) and then add the remaining part. 

 

Best regards, 

VT
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

Oh thanks, i clicked on the link and i read and tried to get it but unfortunately i didn't understand much. 

 

Can you help me with that part of the code? I would really appreciate it if you did! 

 

Lets say i would take number 4 and 5 ... multiply the 4 with 10.. then put 40+5 into a integer called number1 

 

then i put only the number 2 into the second integer called number2 .. 

 

how do i write the code to get the rest from the division 45/2 ? (which is 0.5) using that mega function? 

 

plus, do i need to add extra things in the program for this mega function to work? 

 

Again, Thanks for your time! 

//Reckan
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

You can also use the modulus operator - %. Here is how you can implement it: 

If you take the number 45 and divide it by 2 with the division operator: 45/2, the result will be only the whole part 22, right. After that use the modulus operator with the number 45: 45%2, the result will be 1. If you want to have only one digit after the decimals multiply the remaining part by 10 and than divide it again by 2: 1*10; 10/2 = 5. You should have two separate register for the whole part and the remaining part.  

 

Let's do another example to make things clear: 

If you have 41/7 and you want two digits after the decimals: 

 

reg number1 = 8'd41; reg number2 = 8'd7; reg whole_part = 8'd0; reg remain_part = 8'd0; always@(negedge key3) begin whole_part = number1 / number22; remain_part = ((number1 % number2) * 100) / number2; end  

 

This will return whole_part = 5 and remain_part = 85. 

 

Best regards, 

VT
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

Thank you Victor! 

It works perfect with the modulus operator! I tried that before but i didn't get it to work like i wanted too! 

 

again, thank you!
0 Kudos
Reply