diff --git a/docs/process_rules.rst b/docs/process_rules.rst index c4a380fa..536fa2d8 100644 --- a/docs/process_rules.rst +++ b/docs/process_rules.rst @@ -47,3 +47,24 @@ Parenthesis on multiple lines should be in the same column. rd_full, wr_full ) +process_029 +########### + +This rule checks for **rising_edge** and **falling_edge** in processes. + +**Violation** + +.. code-block:: vhdl + + if (rising_edge(CLK)) then + + if (falling_edge(CLK)) then + +**Fix** + +.. code-block:: vhdl + + if (CLK'event and CLK = '1') then + + if (CLK'event and CLK = '0') then + diff --git a/vsg/rules/process/__init__.py b/vsg/rules/process/__init__.py index 091dd7eb..59579f3b 100644 --- a/vsg/rules/process/__init__.py +++ b/vsg/rules/process/__init__.py @@ -27,3 +27,4 @@ from rule_025 import rule_025 from rule_026 import rule_026 from rule_027 import rule_027 from rule_028 import rule_028 +from rule_029 import rule_029 diff --git a/vsg/rules/process/rule_029.py b/vsg/rules/process/rule_029.py new file mode 100644 index 00000000..c4d93f71 --- /dev/null +++ b/vsg/rules/process/rule_029.py @@ -0,0 +1,30 @@ + +from vsg import rule +from vsg import check +from vsg import fix + +import re + + +class rule_029(rule.rule): + ''' + Process rule 029 checks for "rising_edge" and "falling_edge" in processes. + ''' + + def __init__(self): + rule.rule.__init__(self) + self.name = 'process' + self.identifier = '029' + self.solution = 'Use \'event for clocks.' + self.phase = 1 + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.insideProcess and ('ing_edge' in oLine.lineLower): + self.add_violation(iLineNumber) + + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + oLine = oFile.lines[iLineNumber] + oLine.update_line(re.sub(r'rising_edge\s*\(\s*(\w+)\s*\)', r'\1"event and \1 = "1"', oLine.line, re.IGNORECASE).replace('"', '\'')) + oLine.update_line(re.sub(r'falling_edge\s*\(\s*(\w+)\s*\)', r'\1"event and \1 = "0"', oLine.line, re.IGNORECASE).replace('"', '\'')) diff --git a/vsg/tests/code_examples/PIC.fixed.vhd b/vsg/tests/code_examples/PIC.fixed.vhd index 277c011f..905db56d 100644 --- a/vsg/tests/code_examples/PIC.fixed.vhd +++ b/vsg/tests/code_examples/PIC.fixed.vhd @@ -80,7 +80,7 @@ begin if (RST_I = '1') then next_s <= reset_s; - elsif (rising_edge(CLK_I)) then + elsif (CLK_I'event and CLK_I = '1') then flag <= INTA_I; case next_s is diff --git a/vsg/tests/code_examples/c16/BaudGen.fixed.vhd b/vsg/tests/code_examples/c16/BaudGen.fixed.vhd index 7e1455f0..5df69026 100644 --- a/vsg/tests/code_examples/c16/BaudGen.fixed.vhd +++ b/vsg/tests/code_examples/c16/BaudGen.fixed.vhd @@ -49,7 +49,7 @@ begin process (CLK_I) is begin - if (rising_edge(CLK_I)) then + if (CLK_I'event and CLK_I = '1') then CE_16 <= '0'; -- make CE_16 stay on for (at most) one cycle if (RST_I = '1') then diff --git a/vsg/tests/code_examples/graphicsaccelerator/Bresenhamer.fixed.vhd b/vsg/tests/code_examples/graphicsaccelerator/Bresenhamer.fixed.vhd index 38fe5f5f..d470dd2f 100644 --- a/vsg/tests/code_examples/graphicsaccelerator/Bresenhamer.fixed.vhd +++ b/vsg/tests/code_examples/graphicsaccelerator/Bresenhamer.fixed.vhd @@ -98,7 +98,7 @@ begin process (Clk) is begin - if (rising_edge(Clk)) then + if (Clk'event and Clk = '1') then if (State = IDLE) then if (Reset = '1') then State <= CLEAR; diff --git a/vsg/tests/code_examples/graphicsaccelerator/Debouncer.fixed.vhd b/vsg/tests/code_examples/graphicsaccelerator/Debouncer.fixed.vhd index ac70516e..a1855040 100644 --- a/vsg/tests/code_examples/graphicsaccelerator/Debouncer.fixed.vhd +++ b/vsg/tests/code_examples/graphicsaccelerator/Debouncer.fixed.vhd @@ -33,7 +33,7 @@ begin process (Clk) is begin - if (rising_edge(Clk)) then + if (Clk'event and Clk = '1') then Counter <= nCounter; ButtonHistory <= nButtonHistory; end if; diff --git a/vsg/tests/code_examples/graphicsaccelerator/FrameBuffer2.fixed.vhd b/vsg/tests/code_examples/graphicsaccelerator/FrameBuffer2.fixed.vhd index 50d408a5..f211bbe1 100644 --- a/vsg/tests/code_examples/graphicsaccelerator/FrameBuffer2.fixed.vhd +++ b/vsg/tests/code_examples/graphicsaccelerator/FrameBuffer2.fixed.vhd @@ -45,7 +45,7 @@ begin process (clk) is begin - if (rising_edge(Clk)) then + if (Clk'event and Clk = '1') then if (BufferWrite = '1') then mybuffer(conv_integer(addressWrite)) <= inColor; end if; diff --git a/vsg/tests/code_examples/graphicsaccelerator/FreqDiv.fixed.vhd b/vsg/tests/code_examples/graphicsaccelerator/FreqDiv.fixed.vhd index 6075964d..04fc9280 100644 --- a/vsg/tests/code_examples/graphicsaccelerator/FreqDiv.fixed.vhd +++ b/vsg/tests/code_examples/graphicsaccelerator/FreqDiv.fixed.vhd @@ -22,7 +22,7 @@ begin process (Clk) is begin - if (rising_edge(Clk)) then + if (Clk'event and Clk = '1') then counter <= counter + 1; end if; diff --git a/vsg/tests/code_examples/graphicsaccelerator/Pointer.fixed.vhd b/vsg/tests/code_examples/graphicsaccelerator/Pointer.fixed.vhd index e75aee4a..aa70f4c2 100644 --- a/vsg/tests/code_examples/graphicsaccelerator/Pointer.fixed.vhd +++ b/vsg/tests/code_examples/graphicsaccelerator/Pointer.fixed.vhd @@ -40,7 +40,7 @@ begin process (Clk) is begin - if (rising_edge(Clk)) then + if (Clk'event and Clk = '1') then if (Move = '1') then if (MoveLeft = '1' and MoveRight = '0') then if (not (rX = "0000000000")) then diff --git a/vsg/tests/code_examples/graphicsaccelerator/Synchronizer.fixed.vhd b/vsg/tests/code_examples/graphicsaccelerator/Synchronizer.fixed.vhd index 99b93731..e08caa3b 100644 --- a/vsg/tests/code_examples/graphicsaccelerator/Synchronizer.fixed.vhd +++ b/vsg/tests/code_examples/graphicsaccelerator/Synchronizer.fixed.vhd @@ -54,7 +54,7 @@ begin process (Clk) is begin - if (rising_edge(Clk)) then + if (Clk'event and Clk = '1') then if (XState=TPW and X(7 downto 1)="1100000") then X <= (others=>'0'); XState <= TBP; diff --git a/vsg/tests/process/process_event_test_input.vhd b/vsg/tests/process/process_event_test_input.vhd new file mode 100644 index 00000000..d1cf297b --- /dev/null +++ b/vsg/tests/process/process_event_test_input.vhd @@ -0,0 +1,27 @@ + +architecture RTL of ENTITY1 is + +begin + + PROC_1 : process(CLK) is + begin + + if (rising_edge(CLK)) then + wr_en <= '1'; + end if; + + if (falling_edge(CLK)) then + rd_en <= '1'; + end if; + + if (CLK'event and CLK = '1') then + overflow <= '1'; + end if; + + if (CLK'event and CLK = '0') then + underflow <= '1'; + end if; + + end process PROC_1; + +end architecture RTL; diff --git a/vsg/tests/process/test_fix_rule_process.py b/vsg/tests/process/test_fix_rule_process.py index 4795575d..31f6d833 100644 --- a/vsg/tests/process/test_fix_rule_process.py +++ b/vsg/tests/process/test_fix_rule_process.py @@ -8,6 +8,7 @@ from vsg import vhdlFile # Read in test file used for all tests oFile = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'process_test_input.vhd')) +oFileEvent = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'process_event_test_input.vhd')) class testFixRuleProcessMethods(unittest.TestCase): @@ -201,3 +202,12 @@ class testFixRuleProcessMethods(unittest.TestCase): self.assertEqual(oFile.lines[35].line, ' ) is -- This is a comment') self.assertEqual(oFile.lines[41].line, ' ) is') self.assertEqual(oFile.lines[29].line, ' ) is') + + def test_fix_rule_029(self): + oRule = process.rule_029() + dExpected = [] + oRule.fix(oFileEvent) + oRule.analyze(oFileEvent) + self.assertEqual(oRule.violations, dExpected) + self.assertEqual(oFileEvent.lines[9].line, ' if (CLK\'event and CLK = \'1\') then') + self.assertEqual(oFileEvent.lines[13].line, ' if (CLK\'event and CLK = \'0\') then') diff --git a/vsg/tests/process/test_rule_process.py b/vsg/tests/process/test_rule_process.py index 5f4f8a8b..1adc02e9 100644 --- a/vsg/tests/process/test_rule_process.py +++ b/vsg/tests/process/test_rule_process.py @@ -8,6 +8,7 @@ from vsg import vhdlFile # Read in test file used for all tests oFile = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','process','process_test_input.vhd')) +oFileEvent = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'process_event_test_input.vhd')) class testRuleProcessMethods(unittest.TestCase): @@ -262,3 +263,12 @@ class testRuleProcessMethods(unittest.TestCase): dExpected = [27,33] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) + + def test_rule_029(self): + oRule = process.rule_029() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '029') + dExpected = [9,13] + oRule.analyze(oFileEvent) + self.assertEqual(oRule.violations, dExpected)