Source file cordic.ml
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
open Base
open Hardcaml
include Cordic_intf
let atanh t = 0.5 *. Float.log ((1.0 +. t) /. (1.0 -. t))
module System = struct
include Cordic_reference.System
let const = function
| Circular -> "00"
| Hyperbolic -> "01"
| Linear -> "10"
;;
let to_signal t = Signal.of_string (const t)
end
module Mode = struct
include Cordic_reference.Mode
let const = function
| Rotation -> "00"
| Vectoring -> "01"
| Inverse -> "11"
;;
let to_signal t = Signal.of_string (const t)
end
module Make (Fixnum_spec : Fixnum.Spec) = struct
module Fixnum = Fixnum.Make (Fixnum_spec)
module Make_unrolled (B : Comb.S) = struct
let iter = Cordic_reference.iter
let atan iterations =
Array.init iterations ~f:(fun i -> Fixnum.of_float Float.(atan (2. **. -of_int i)))
;;
let atanh iterations =
iter ~iterations ~init:[] ~f:(fun ~i:_ ~ih l ->
Fixnum.of_float Float.(atanh (2. **. -of_int ih)) :: l)
|> Array.of_list_rev
;;
let t iterations = Array.init iterations ~f:(fun i -> Fixnum.pow2 (-i))
let step ~x ~xsft ~y ~ysft ~z ~d ~m ~e =
let open B in
let x' = mux2 (msb m) x (mux2 (lsb m ^: d) (x +: ysft) (x -: ysft)) in
let y' = mux2 d (y -: xsft) (y +: xsft) in
let z' = mux2 d (z +: e) (z -: e) in
x', y', z'
;;
let cordic ?(pipe = Fn.id) ~system ~mode ~iterations ~c ~x ~y ~z () =
let open B in
assert (width x = width y);
assert (width x = width z);
let m = system in
let e = [ atan iterations; atanh iterations; t iterations ] in
let is_hyper = system ==: B.constb (System.const Hyperbolic) in
iter ~iterations ~init:(x, y, z) ~f:(fun ~i ~ih (x, y, z) ->
let e =
mux system (List.map e ~f:(fun e -> B.constb (e.(i) |> Fixnum.constb)))
in
let d = mux mode [ z <+. 0; y >=+. 0; y >=+ c ] in
let xsft = mux2 is_hyper (sra x ih) (sra x i) in
let ysft = mux2 is_hyper (sra y ih) (sra y i) in
let x, y, z = step ~x ~xsft ~y ~ysft ~z ~d ~m ~e in
pipe x, pipe y, pipe z)
;;
end
module Iterative = struct
module S = Signal
module C = Make_unrolled (S)
open S
let hyper_iter ~reg_spec ~enable ~ld ~system ~iter =
let is_hyper = system ==: Signal.constb (System.const Hyperbolic) in
let iterh = wire (width iter) -- "iterh" in
let k = wire (width iter + 2) -- "k" in
let repeat = (k ==: zero 2 @: iterh) -- "repeated_step" in
let upd v ~init t f =
v
<== reg
reg_spec
~enable
(mux2 ld (of_int ~width:(width t) init) (mux2 repeat t f))
in
upd k ~init:4 (k +: sll k 1 +:. 1) k;
upd iterh ~init:1 iterh (iterh +:. 1);
mux2 is_hyper iterh iter -- "iter_sel"
;;
let cordic ~reg_spec ~enable ~ld ~system ~mode ~iterations ~c ~x ~y ~z =
let wi = num_bits_to_represent (iterations - 1) in
let iter =
reg_fb reg_spec ~enable ~width:wi ~f:(fun d -> mux2 ld (zero wi) (d +:. 1))
-- "iter"
in
let table_lookup table =
mux
iter
(Array.to_list (Array.map table ~f:(fun c -> S.constb (c |> Fixnum.constb))))
in
let atan = table_lookup (C.atan iterations) in
let atanh = table_lookup (C.atanh iterations) in
let t = table_lookup (C.t iterations) in
let xw, yw, zw = wire Fixnum.width, wire Fixnum.width, wire Fixnum.width in
let m = system in
let e = mux system [ atan; atanh; t ] -- "e" in
let d = mux mode [ zw <+. 0; yw >=+. 0; yw >=+ c ] in
let iter = hyper_iter ~reg_spec ~enable ~ld ~system ~iter in
let xsft = log_shift sra xw iter in
let ysft = log_shift sra yw iter in
let xs, ys, zs = C.step ~x:xw ~xsft ~y:yw ~ysft ~z:zw ~d ~m ~e in
xw <== reg reg_spec ~enable (mux2 ld x xs);
yw <== reg reg_spec ~enable (mux2 ld y ys);
zw <== reg reg_spec ~enable (mux2 ld z zs);
xw, yw, zw
;;
end
module Unrolled = Make_unrolled (Signal)
module I = struct
type 'a t =
{ clk : 'a
; clr : 'a
; enable : 'a [@bits 1]
; ld : 'a [@bits 1]
; system : 'a [@bits 2]
; mode : 'a [@bits 2]
; c : 'a [@bits Fixnum.width]
; x : 'a [@bits Fixnum.width]
; y : 'a [@bits Fixnum.width]
; z : 'a [@bits Fixnum.width]
}
[@@deriving sexp_of, hardcaml]
end
module O = struct
type 'a t =
{ xo : 'a [@bits Fixnum.width]
; yo : 'a [@bits Fixnum.width]
; zo : 'a [@bits Fixnum.width]
}
[@@deriving sexp_of, hardcaml]
end
let create (config : Config.t) (i : Signal.t I.t) =
let reg_spec = Reg_spec.create () ~clock:i.clk ~clear:i.clr in
let iterations = config.iterations in
let xo, yo, zo =
let system, mode = i.system, i.mode in
let x, y, z, c = i.x, i.y, i.z, i.c in
match config.architecture with
| Combinational -> Unrolled.cordic ~system ~mode ~iterations ~c ~x ~y ~z ()
| Pipelined ->
Unrolled.cordic
~pipe:(Signal.reg reg_spec ~enable:i.enable)
~system
~mode
~iterations
~c
~x
~y
~z
()
| Iterative ->
Iterative.cordic
~reg_spec
~enable:i.enable
~ld:i.ld
~system
~mode
~iterations
~c
~x
~y
~z
in
{ O.xo; yo; zo }
;;
end