Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Verilog to VHDL translation

Status
Not open for further replies.

Ponta

Newbie level 2
Newbie level 2
Joined
Jan 7, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
15
Hello, can somebody please translate this verilog code to VHDL?
Thanks
Code:
module PWM(clock, switches, pwm
);

parameter sd=195;

input clock;
input [7:0] switches;

output pwm;
reg pwm;

reg [15:0] counter = 0;

always @ (posedge clock)
begin
counter = counter+1;
if(counter<=switches*sd) pwm = 1;
else pwm = 0;
if(counter>= 50_000) counter = 0;
end
endmodule
 

Do you know that the "counter <= switches*sd" in the if represents a large amount of logic? You're multiplying two 8-bit values and comparing the 16-bit result to a counter. Don't expect that to be super fast.

The code is also using blocking assignments, which is a bad coding practice in a edge triggered procedural block.

I also don't like the following code:
Code:
counter = counter+1;
if (counter>=50000) counter = 0;
I don't like to rely on the tools to interpret what I want, increment count, WAIT the count is 50000 or greater! clear the count instead!. I'd rather explicitly tell it what I want:
Code:
if (counter >= 50000) counter <= 0;
else counter <= counter +1;
which does the same thing, but leaves nothing to interpretation.
 
  • Like
Reactions: Ponta

    Ponta

    Points: 2
    Helpful Answer Positive Rating
I figured nobody cares if they don't know how to count clocks, so I didn't bother to point that out. It seems like I'm always pointing that problem out.

- - - Updated - - -

BTW here is a translation.

Code VHDL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
 
entity PWM is
  generic
  (
    sd        : unsigned(7 downto 0) := X"C3" -- 195 in hex
  );
  port
  (
    clock     : in   std_logic;
    switches  : in   unsigned(7 downto 0);
    pwm       : out  std_logic
  );
end PWM;
 
architecture behave_PWM of PWM is
  signal counter : unsigned(15 downto 0);
 
begin -- architecture
  pwm_proc: process (clock)
  begin
    if (counter <= switches*sd) then
      pwm <= '1';
    else
      pwm <= '0';
    end if;
 
    if (counter >= 50000) then
      counter <= (others => '0');
    else
      counter <= counter + 1;
    end if;
  end process;
 
end behave_PWM;


It could have errors, but it does compile cleanly using Vivado's xvhdl.
 
  • Like
Reactions: Ponta

    Ponta

    Points: 2
    Helpful Answer Positive Rating
Thank you for your answers. Can you tell me should this work when implemented on hardware? The task is to change brightness of LED's by switching 8 switches?
I don't have the board at home, and it's hard for me to go and check it at my university at this time.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top