summaryrefslogtreecommitdiff
path: root/testsuite/gna/sr2940/GCD.vhd
blob: 509b72162dc81765ff12f94ec3c2375d668b4be8 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
-- module GCD where
-- 
-- 
-- $wmygcd::*Int# -> *Int# -> *Int#
-- $wmygcd ww ww1 =
--   let wild::GHC.Types.Bool = (GHC.Prim.==# ww ww1) in
--   case wild of :: *Int#
--     GHC.Types.False ->
--       let wild1::GHC.Types.Bool = (GHC.Prim.<# ww ww1) in
--       case wild1 of :: *Int#
--         GHC.Types.False -> ($wmygcd (GHC.Prim.-# ww ww1) ww1)
--         GHC.Types.True -> ($wmygcd ww (GHC.Prim.-# ww1 ww))
--     GHC.Types.True -> ww
-- 
-- mygcd::GHC.Types.Int -> GHC.Types.Int -> GHC.Types.Int
-- mygcd w w1 =
--   let w2::GHC.Types.Int = w in
--   case w2 of :: GHC.Types.Int
--     GHC.Types.I# ww::*Int# ->
--       let w3::GHC.Types.Int = w1 in
--       case w3 of :: GHC.Types.Int
--         GHC.Types.I# ww1::*Int# ->
--           let ww2::*Int# = ($wmygcd ww ww1) in
--           case ww2 of :: GHC.Types.Int DEFAULT -> (GHC.Types.I# ww2)
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.\Prim\.all;

package \GCD\ is
end \GCD\;

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.\Prim\.all;
use work.\GCD\.all;

entity \$wmygcd\ is
  port (clk : in std_logic;
        s1_call : in std_logic;
        s1_ret : out std_logic;
        s1_ww : in \Int#\;
        s1_ww1 : in \Int#\;
        res : out \Int#\);
end entity;

architecture rtl of \$wmygcd\ is
  signal tail_call : std_logic;
  signal tail_ww : \Int#\;
  signal tail_ww1 : \Int#\;
  signal core_call : std_logic;
  signal core_ret : std_logic;
  signal core_ww : \Int#\;
  signal core_ww1 : \Int#\;
  signal s1_act : std_logic;
  signal s1_wait : std_logic;
  signal s1_saved_ww : \Int#\;
  signal s1_saved_ww1 : \Int#\;
begin
  process (core_call, core_ww, core_ww1)
  variable wild : \GHC.Types.Bool\;
  variable wild1 : \GHC.Types.Bool\;
  variable ww : \Int#\;
  variable ww1 : \Int#\;
  begin
    ww := core_ww;
    ww1 := core_ww1;
    wild := \GHC.Prim.==#\(ww, ww1);
    if \is_GHC.Types.False\(wild) then
      wild1 := \GHC.Prim.<#\(ww, ww1);
      if \is_GHC.Types.False\(wild1) then
        res <= \$wmygcd\(\GHC.Prim.-#\(ww, ww1), ww1);
      elsif \is_GHC.Types.True\(wild1) then
        res <= \$wmygcd\(ww, \GHC.Prim.-#\(ww1, ww));
      end if;
    elsif \is_GHC.Types.True\(wild) then res <= ww;
    end if;
  end process;
  
  process (clk)
  begin
    if rising_edge(clk) then
      core_call <= '0';
      if s1_call = '1' then
        s1_wait <= '1';
        s1_saved_ww <= s1_ww;
        s1_saved_ww1 <= s1_ww1;
      end if;
      if tail_call = '1' then
        core_call <= '1';
        core_ww <= tail_ww;
        core_ww1 <= tail_ww1;
      elsif core_ret = '1' or s1_act = '1' then
        s1_act <= '0';
        if s1_wait = '1' then
          core_call <= '1';
          s1_act <= '1';
          s1_wait <= '0';
          core_ww <= s1_saved_ww;
          core_ww1 <= s1_saved_ww1;
        elsif s1_call = '1' then
          core_call <= '1';
          s1_act <= '1';
          s1_wait <= '0';
          core_ww <= s1_ww;
          core_ww1 <= s1_ww1;
        end if;
      end if;
    end if;
  end process;
  
  s1_ret <= core_ret and s1_act;
  
end architecture;

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.\Prim\.all;
use work.\GCD\.all;

entity mygcd is
  port (clk : in std_logic;
        s1_call : in std_logic;
        s1_ret : out std_logic;
        s1_w : in \GHC.Types.Int\;
        s1_w1 : in \GHC.Types.Int\;
        res : out \GHC.Types.Int\);
end entity;

architecture rtl of mygcd is
  signal tail_call : std_logic;
  signal tail_w : \GHC.Types.Int\;
  signal tail_w1 : \GHC.Types.Int\;
  signal core_call : std_logic;
  signal core_ret : std_logic;
  signal core_w : \GHC.Types.Int\;
  signal core_w1 : \GHC.Types.Int\;
  signal s1_act : std_logic;
  signal s1_wait : std_logic;
  signal s1_saved_w : \GHC.Types.Int\;
  signal s1_saved_w1 : \GHC.Types.Int\;
begin
  process (core_call, core_w, core_w1)
  variable w2 : \GHC.Types.Int\;
  variable ww : \Int#\;
  variable w3 : \GHC.Types.Int\;
  variable ww1 : \Int#\;
  variable ww2 : \Int#\;
  variable w : \GHC.Types.Int\;
  variable w1 : \GHC.Types.Int\;
  begin
    w := core_w;
    w1 := core_w1;
    w2 := w;
    if \is_GHC.Types.I#\(w2) then
      \expand_GHC.Types.I#\(w2, ww);
      w3 := w1;
      if \is_GHC.Types.I#\(w3) then
        \expand_GHC.Types.I#\(w3, ww1);
        ww2 := \$wmygcd\(ww, ww1);
        res <= \GHC.Types.I#\(ww2);
      end if;
    end if;
  end process;
  
  process (clk)
  begin
    if rising_edge(clk) then
      core_call <= '0';
      if s1_call = '1' then
        s1_wait <= '1';
        s1_saved_w <= s1_w;
        s1_saved_w1 <= s1_w1;
      end if;
      if tail_call = '1' then
        core_call <= '1';
        core_w <= tail_w;
        core_w1 <= tail_w1;
      elsif core_ret = '1' or s1_act = '1' then
        s1_act <= '0';
        if s1_wait = '1' then
          core_call <= '1';
          s1_act <= '1';
          s1_wait <= '0';
          core_w <= s1_saved_w;
          core_w1 <= s1_saved_w1;
        elsif s1_call = '1' then
          core_call <= '1';
          s1_act <= '1';
          s1_wait <= '0';
          core_w <= s1_w;
          core_w1 <= s1_w1;
        end if;
      end if;
    end if;
  end process;
  
  s1_ret <= core_ret and s1_act;
  
end architecture;