Skip to content

Commit 18f7f25

Browse files
committed
A slightly interesting VHDL generator
It can now generate a multiplexer from multiplexer.clj. Indentation is a mess at this point
1 parent 1ede3d1 commit 18f7f25

File tree

5 files changed

+156
-8
lines changed

5 files changed

+156
-8
lines changed

mux.vhd

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-------------------------------------------------
2+
-- VHDL code for 4:1 multiplexor
3+
-- (ESD book figure 2.5)
4+
-- by Weijun Zhang, 04/2001
5+
--
6+
-- Multiplexor is a device to select different
7+
-- inputs to outputs. we use 3 bits vector to
8+
-- describe its I/O ports
9+
-------------------------------------------------
10+
11+
library ieee;
12+
use ieee.std_logic_1164.all;
13+
14+
-------------------------------------------------
15+
16+
entity Mux is
17+
port( I3: in std_logic_vector(2 downto 0);
18+
I2: in std_logic_vector(2 downto 0);
19+
I1: in std_logic_vector(2 downto 0);
20+
I0: in std_logic_vector(2 downto 0);
21+
S: in std_logic_vector(1 downto 0);
22+
O: out std_logic_vector(2 downto 0)
23+
);
24+
end Mux;
25+
26+
-------------------------------------------------
27+
28+
architecture behv1 of Mux is
29+
begin
30+
process(I3,I2,I1,I0,S)
31+
begin
32+
33+
-- use case statement
34+
case S is
35+
when "00" => O <= I0;
36+
when "01" => O <= I1;
37+
when "10" => O <= I2;
38+
when "11" => O <= I3;
39+
when others => O <= "ZZZ";
40+
end case;
41+
42+
end process;
43+
end behv1;
44+
45+
architecture behv2 of Mux is
46+
begin
47+
48+
-- use when.. else statement
49+
O <= I0 when S="00" else
50+
I1 when S="01" else
51+
I2 when S="10" else
52+
I3 when S="11" else
53+
"ZZZ";
54+
55+
end behv2;
56+
--------------------------------------------------

src/computer-build/vhdl.clj

-8
This file was deleted.
File renamed without changes.

src/computer_build/vhdl.clj

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
(ns computer_build.vhdl)
2+
3+
(defn lines [& strings]
4+
(apply str (interpose "\n" strings))
5+
)
6+
7+
(defn indented-lines [indent & strings]
8+
(apply lines (map #(str " " %) strings))
9+
)
10+
11+
(defn spaces [& strings]
12+
(apply str (interpose " " strings))
13+
)
14+
15+
(defn keyword-to-str [sym]
16+
(if (keyword? sym) (apply str (rest (str sym))) (str \" sym \"))
17+
)
18+
19+
(defmulti to-vhdl first)
20+
(defmethod to-vhdl :default [arg] "unimplemented")
21+
(defmethod to-vhdl 'entity [entity]
22+
(let [[type name ports & architecture] entity]
23+
(lines
24+
(spaces "entity" name "is")
25+
"port("
26+
;(spaces (concat '(port) (first ports)))
27+
;(interpose " " (concat '(port) (first ports)))
28+
(apply str (interpose ";\n" (map to-vhdl (map #(cons 'port %) ports))))
29+
");"
30+
(str "end " name ";")
31+
32+
(str "architecture arch_" name " of " name " is")
33+
"begin"
34+
(apply lines (map to-vhdl architecture))
35+
(str "end arch_" name ";")
36+
37+
;(lines (map to-vhdl (map ports #(concat '(port) %))))
38+
)))
39+
(defmethod to-vhdl 'port [port]
40+
(let [[type id direction type] port]
41+
(str " " (keyword-to-str id) ": " (keyword-to-str direction) " " type)
42+
))
43+
44+
(defmethod to-vhdl 'process [process]
45+
(let [[type ports & definition] process]
46+
(indented-lines 1
47+
(str "process(" (apply str (interpose "," (map keyword-to-str ports))) ")")
48+
"begin"
49+
(apply lines (map to-vhdl definition))
50+
"end process;"
51+
)))
52+
53+
(defmethod to-vhdl 'case [statement]
54+
(let [[type target & cases] statement]
55+
(indented-lines 2
56+
(spaces "case" (keyword-to-str target) "is")
57+
;(lines (map #(str "when" (str "\"" (first %) "\""))
58+
(apply lines (map #(str "when \"" (first %) "\" => " (to-vhdl (second %)) \;)
59+
(partition 2 cases)))
60+
"end case;"
61+
)))
62+
63+
(defmethod to-vhdl '<= [statement]
64+
(let [[type target expression] statement]
65+
(str (keyword-to-str target) " <= " (keyword-to-str expression))
66+
))
67+
68+
(defn generate-vhdl [& entities]
69+
(do
70+
(println "library ieee;")
71+
(println "use ieee.std_logic_1164.all;")
72+
(println (to-vhdl (first entities)))
73+
;(println (lines (map to-vhdl entities)))
74+
)
75+
)

src/multiplexer.clj

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(use 'computer_build.vhdl)
2+
3+
(generate-vhdl
4+
'(entity "Mux"
5+
;; ports
6+
[
7+
[:I3 :in "std_logic_vector(2 downto 0)"]
8+
[:I2 :in "std_logic_vector(2 downto 0)"]
9+
[:I1 :in "std_logic_vector(2 downto 0)"]
10+
[:I0 :in "std_logic_vector(2 downto 0)"]
11+
[:S :in "std_logic_vector(1 downto 0)"]
12+
[:O :out "std_logic_vector(2 downto 0)"]
13+
]
14+
;; architecture
15+
(process [:I3 :I2 :I1 :I0 :S]
16+
(case :S
17+
"00" (<= :O :I0)
18+
"01" (<= :O :I1)
19+
"10" (<= :O :I2)
20+
"11" (<= :O :I3)
21+
"others" (<= :O "ZZZ")
22+
)
23+
)
24+
)
25+
)

0 commit comments

Comments
 (0)