summaryrefslogtreecommitdiffstats
path: root/lab3/lab.vhd
blob: d699e49ea5fe996f74c915605bb7d3ff7e9f1656 (plain) (blame)
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
-- slå på fpga
-- stäng av modelsim
-- make lab.prog

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.all;


entity lab is
    Port ( clk, rst, rx : in  STD_LOGIC;    -- rst är tryckknappen i mitten under displayen
           seg: out  UNSIGNED(7 downto 0);
           an : out  UNSIGNED (3 downto 0));
end lab;

architecture Behavioral of lab is
    component leddriver
        Port ( clk,rst : in  STD_LOGIC;
               seg : out  UNSIGNED(7 downto 0);
               an : out  UNSIGNED (3 downto 0);
               value : in  UNSIGNED (15 downto 0));
    end component;

    signal sreg : UNSIGNED(9 downto 0) := B"0_00000000_0";  -- 10 bit skiftregister
    signal tal : UNSIGNED(15 downto 0) := X"0000";
    signal rx1,rx2 : std_logic;         -- vippor på insignalen
    signal sp : std_logic;              -- skiftpuls
    signal lp : std_logic;              -- laddpuls
    signal pos : UNSIGNED(1 downto 0) := "00";

    signal bit_cycle : UNSIGNED(11 downto 0) := X"000";  -- fits at least 868
    signal bits_received : UNSIGNED(3 downto 0) := X"0";
    signal receiving : std_logic; -- currently receiving a byte

begin
    -- *****************************
    -- *  synkroniseringsvippor    *
    -- *****************************
    -- 1 process
    --
    -- Förändringarna på insignalen ska komma i takt med vår klocka.

    process(clk) begin
        if rising_edge(clk) then
            if rst='1' then
                rx1 <= '0';
                rx2 <= '0';
            else
                rx1 <= rx;
                rx2 <= rx1;
            end if;
        end if;
    end process;


    -- *****************************
    -- *       styrenhet           *
    -- *****************************
    -- 1 eller 2 processer
    --
    -- Denna producerar två styrsignaler, båda enpulsade:
    -- – Skiftpulsen sp, som kommer mitt i (ungefär) varje bit.
    -- – Laddpulsen lp, som kommer efter den sista skiftpulsen.

    process(clk) begin
        if rising_edge(clk) then
            if rst = '1' then
                bit_cycle <= X"000";
                bits_received <= X"0";
                receiving <= '0';
            elsif receiving = '1' then
                -- have we waited for an uart cycle?
                if bit_cycle = X"364" then
                    -- yes. read uart bit
                    if bits_received = X"9" then
                        receiving <= '0';
                        bits_received <= X"0";
                    else
                        bit_cycle <= X"000";
                        bits_received <= bits_received + 1;
                    end if;
                else
                    bit_cycle <= bit_cycle + 1;
                end if;
            else -- not receiving
                if rx2 = '0' and rx1 = '1' then
                    -- received a start bit so start receiving
                    receiving <= '1';
                    bit_cycle <= X"1b2"; -- half, so we read in the middle
                    bits_received <= X"0";
                else
                    receiving <= '0';
                end if;
            end if;
        end if;
    end process;

    process(clk) begin
        if rising_edge(clk) then
            if rst='1' then
                lp <= '0';
                sp <= '0';
            else
                if (receiving = '1') and (bit_cycle = X"364") then
                    sp <= '1';
                else
                    sp <= '0';
                end if;
                if (sp = '1') and (receiving = '0') then
                    lp <= '1'
                else
                    lp <= '0';
                end if;
            end if;
        end if;
    end process;


    -- *****************************
    -- * 10 bit skiftregister      *
    -- *****************************
    -- 1 process
    --
    -- De 10 bitarna i varje siffra skiftas in i skiftregistret.

    process(clk) begin
        if rising_edge(clk) then
            if rst = '1' then
                sreg <= B"0_000_000_0";
            elsif sp = '1' then
                sreg(0) <= sreg(1);
                sreg(1) <= sreg(2);
                sreg(2) <= sreg(3);
                sreg(3) <= sreg(4);
                sreg(4) <= sreg(5);
                sreg(5) <= sreg(6);
                sreg(6) <= sreg(7);
                sreg(7) <= sreg(8);
                sreg(8) <= sreg(9);
                sreg(9) <= rx2;
            end if;
        end if;
    end process;


    -- *****************************
    -- * 2  bit räknare            *
    -- *****************************
    -- 1 process

    process(clk) begin
        if rising_edge(clk) then
            if rst='1' then
                pos <= "00";
            elsif lp = '1' then
                if pos = "11" then
                    pos <= "00";
                else
                    pos <= pos + 1;
                end if;
            else
            end if;
        end if;
    end process;


    -- *****************************
    -- * 16 bit register           *
    -- *****************************
    -- 1 process
    --
    -- ... för 4 siffror. Laddas av laddpulsen, samtidigt räknas räknaren upp.

    process(clk) begin
        if rising_edge(clk) then
            if rst='1' then
                tal <= X"0000";
            elsif lp = '1' then
                if pos = "00" then
                    tal(3 downto 0) <= unsigned(sreg(8 downto 1));
                elsif pos = "01" then
                    tal(7 downto 4) <= unsigned(sreg(8 downto 1));
                elsif pos = "10" then
                    tal(11 downto 8) <= unsigned(sreg(8 downto 1));
                else
                    tal(15 downto 12) <= unsigned(sreg(8 downto 1));
                end if;
            end if;
        end if;
    end process;


    -- *****************************
    -- * Multiplexad display       *
    -- *****************************
    -- Inkoppling av komponenten leddriver
    led: leddriver port map (clk, rst, seg, an, tal);

end Behavioral;