-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbuiltins.v
1486 lines (1112 loc) · 40 KB
/
builtins.v
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
`define assert(signal, value)
//if ((signal) !== (value)) begin $display("ASSERTION FAILED in %m: signal != value"); $finish(1); end
module load();
endmodule // load
module store();
endmodule // store
module minOp(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [0:0] out);
parameter WIDTH = 1;
assign out = in0 >= in1 ? in1 : in0;
endmodule
module maxOp(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [0:0] out);
parameter WIDTH = 1;
assign out = in0 >= in1 ? in0 : in1;
endmodule
module ne(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [0:0] out);
parameter WIDTH = 1;
assign out = in0 != in1;
endmodule
module sext(input [31:0] in, output [63:0] out);
assign out = {32'b0, in};
endmodule
module zext(input [IN_WIDTH - 1:0] in, output [OUT_WIDTH - 1:0] out);
parameter IN_WIDTH = 32;
parameter OUT_WIDTH = 64;
assign out = {{(OUT_WIDTH-IN_WIDTH){1'b0}},in};
endmodule
module shlOp(input [WIDTH - 1:0] in0, input [$clog2(WIDTH) - 1 : 0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 32;
assign out = in0 << in1;
endmodule
module lshrOp(input [WIDTH - 1:0] in0, input [$clog2(WIDTH) - 1 : 0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 32;
assign out = in0 >> in1;
endmodule
module ashrOp(input [WIDTH - 1:0] in0, input [$clog2(WIDTH) - 1 : 0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 32;
assign out = in0 >>> in1;
endmodule
module trunc(input [IN_WIDTH - 1:0] in, output [OUT_WIDTH - 1:0] out);
parameter IN_WIDTH = 32;
parameter OUT_WIDTH = 32;
assign out = in[OUT_WIDTH - 1 : 0];
endmodule
module notOp(input [WIDTH - 1:0] in, output [WIDTH - 1:0] out);
parameter WIDTH = 32;
assign out = ~in;
endmodule
module add(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 1;
assign out = in0 + in1;
endmodule
module andOp(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 1;
assign out = in0 & in1;
endmodule
module orOp(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 1;
assign out = in0 | in1;
endmodule
module sub(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 1;
assign out = in0 - in1;
endmodule
module mul(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 1;
assign out = in0 * in1;
endmodule
module getelementptr_1(input [31:0] base_addr, input [31:0] in1, output [31:0] out);
assign out = base_addr + in1;
endmodule
module getelementptr_2(input [31:0] base_addr, input [31:0] in1, input [31:0] in2, output [31:0] out);
assign out = base_addr + in1 + in2;
endmodule
module eq(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [0:0] out);
parameter WIDTH = 1;
assign out = in0 == in1;
endmodule
module sgt(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [0:0] out);
parameter WIDTH = 1;
always @(*) begin
$display("sgt: in0 = %b, in1 = %b, out = %b", in0, in1, out);
end
assign out = $signed(in0) > $signed(in1);
endmodule
module slt(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [0:0] out);
parameter WIDTH = 1;
assign out = $signed(in0) < $signed(in1);
endmodule
module ult(input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [0:0] out);
parameter WIDTH = 1;
assign out = $unsigned(in0) < $unsigned(in1);
endmodule // ult
module sremOp(input [WIDTH - 1 : 0] in0, input [WIDTH - 1 : 0] in1, output [WIDTH - 1 : 0] out);
parameter WIDTH = 32;
assign out = $signed(in0) % $signed(in1);
endmodule
module sliceOp(input [IN_WIDTH - 1 : 0] in, output [OUT_WIDTH - 1 : 0] out);
parameter IN_WIDTH = 32;
parameter OUT_WIDTH = 32;
parameter OFFSET = 0;
always @(*) begin
$display("in = %d", in);
end
assign out = in[OFFSET + OUT_WIDTH - 1 : OFFSET];
endmodule // sliceOp
module mixOp(input [IN0_WIDTH - 1 : 0] main,
input [IN1_WIDTH - 1 : 0] inner,
output [IN0_WIDTH - 1 : 0] out);
parameter IN0_WIDTH = 32;
parameter IN1_WIDTH = 1;
parameter MAIN_OFFSET = 10;
//assign out = {main[IN0_WIDTH - 1 : IN1_WIDTH + MAIN_OFFSET], inner, main[MAIN_OFFSET - 1 : 0]};
generate
if (MAIN_OFFSET == 0)
assign out = {main[IN0_WIDTH - 1 : IN1_WIDTH + MAIN_OFFSET], inner};
else
assign out = {main[IN0_WIDTH - 1 : IN1_WIDTH + MAIN_OFFSET], inner, main[MAIN_OFFSET - 1 : 0]};
endgenerate
endmodule
module ret();
endmodule
// module phi_2(input [31:0] last_block,
// input [31:0] s0,
// input [31:0] s1,
// input [31:0] in0,
// input [31:0] in1,
// output [31:0] out);
// reg [31:0] out_reg;
// always @(*) begin
// //$display("In phi: last_block == %d, but s0 == %d, and s1 == %d", last_block, s0, s1);
// if (last_block == s0) begin
// out_reg = in0;
// end else if (last_block == s1) begin
// out_reg = in1;
// end else begin
// $display("Error: last_block == %d, but s0 == %d, and s1 == %d", last_block, s0, s1);
// end
// // else begin
// // $display("Error: last_block == %d, but s0 == %d, and s1 == %d", last_block, s0, s1);
// // $finish();
// // end
// end
// assign out = out_reg;
// endmodule
// module phi_3(input [31:0] last_block,
// input [31:0] s0,
// input [31:0] s1,
// input [31:0] s2,
// input [31:0] in0,
// input [31:0] in1,
// input [31:0] in2,
// output [31:0] out);
// reg [31:0] out_reg;
// always @(*) begin
// if (last_block == s0) begin
// out_reg = in0;
// end else if (last_block == s1) begin
// out_reg = in1;
// end else if (last_block == s2) begin
// out_reg = in2;
// end else begin
// $display("Error: last_block == %d, but s0 == %d, and s1 == %d, and s2== %d", last_block, s0, s1, s2);
// end
// end
// assign out = out_reg;
// endmodule
module br_dummy();
endmodule
module select(input sel, input [WIDTH - 1:0] in0, input [WIDTH - 1:0] in1, output [WIDTH - 1:0] out);
parameter WIDTH = 1;
assign out = sel ? in1 : in0;
endmodule
module axi_write_handler(input clk,
input rst,
// User facing API
input [DATA_WIDTH - 1 : 0] write_data,
input [ADDR_WIDTH - 1 : 0] write_addr,
input start_write,
output ready,
output valid,
// AXI module API
output reg s_axil_awvalid,
output reg s_axil_wvalid,
output reg [DATA_WIDTH - 1 : 0] s_axil_wdata,
output reg [ADDR_WIDTH - 1 : 0] s_axil_awaddr,
input s_axil_bvalid,
input [1:0] s_axil_bresp,
output [STRB_WIDTH-1:0] s_axil_wstrb,
output s_axil_bready
);
parameter DATA_WIDTH = 32;
parameter ADDR_WIDTH = 5;
parameter STRB_WIDTH = (DATA_WIDTH/8);
reg ready_reg;
reg valid_reg;
assign ready = ready_reg;
assign valid = valid_reg;
assign s_axil_bready = 1'b1;
assign s_axil_wstrb = 5'b11111;
always @(posedge clk) begin
if (rst) begin
ready_reg <= 1;
valid_reg <= 0;
s_axil_wvalid <= 0;
s_axil_awvalid <= 0;
//$display("Reset");
end else if (start_write) begin
//$display("starting write to %d", write_addr);
s_axil_wvalid <= 1;
s_axil_awvalid <= 1;
s_axil_wdata <= write_data;
s_axil_awaddr <= {write_addr, 2'b0};
ready_reg <= 0;
valid_reg <= 0;
end else if (s_axil_bvalid && (!s_axil_bresp)) begin
//$display("s_axil_bvalid = %d", s_axil_bvalid);
//$display("write is done and valid");
ready_reg <= 1;
valid_reg <= 1;
s_axil_wvalid <= 0;
s_axil_awvalid <= 0;
end
end
endmodule // axi_write_handler
module axi_read_handler(input clk,
input rst,
// User facing API
output reg [DATA_WIDTH - 1 : 0] read_data,
input [ADDR_WIDTH - 1 : 0] read_addr,
input start_read,
output reg ready,
output reg valid,
// AXI facing API
output reg s_axil_rready,
output reg s_axil_arvalid,
output reg [ADDR_WIDTH - 1 : 0] s_axil_araddr,
input s_axil_rvalid,
input [1:0] s_axil_rresp,
input s_axil_arready,
input [DATA_WIDTH - 1 : 0] s_axil_rdata);
parameter DATA_WIDTH = 32;
parameter ADDR_WIDTH = 5;
parameter STRB_WIDTH = (DATA_WIDTH/8);
always @(posedge clk) begin
// $display("&&&&&&");
// $display("s_axil_rvalid === %d", s_axil_rvalid);
// $display("s_axil_rresp === %d", s_axil_rresp);
// $display("s_axil_arready === %d", s_axil_arready);
// $display("======");
if (rst) begin
ready <= 1;
valid <= 0;
s_axil_arvalid <= 0;
s_axil_rready <= 0;
end else if (start_read) begin
//$display("starting read to %d", read_addr);
valid <= 0;
ready <= 0;
s_axil_rready <= 1;
s_axil_arvalid <= 1;
s_axil_araddr <= {read_addr, 2'b0};
end else if (s_axil_arready && s_axil_rvalid && (s_axil_rresp == 0)) begin
//$display("Setting read output valid, data = %d", s_axil_rdata);
read_data <= s_axil_rdata;
valid <= 1;
end else begin
valid <= 0;
s_axil_arvalid <= 0;
end
end
endmodule // axi_read_handler
module axi_stall_manager(input clk,
input rst,
input start_read,
input start_write,
input read_finished,
input write_finished,
output should_stall);
reg reading;
reg writing;
always @(posedge clk) begin
if (rst) begin
reading <= 0;
writing <= 0;
end else begin
// $display("start write = %d", start_write);
// $display("writing = %d", writing);
// $display("should stall = %d", should_stall);
if (start_read) begin
reading <= 1;
end
if (start_write) begin
// $display("writing...");
writing <= 1;
end
if (read_finished) begin
reading <= 0;
end
if (write_finished) begin
// $display("write finished");
writing <= 0;
end
end
end // always @ (posedge clk)
assign should_stall = reading | writing;
endmodule // axi_stall_manager
module register(input clk, input rst, input [WIDTH - 1:0] raddr, input [WIDTH - 1:0] waddr, input wen, input ren, input [WIDTH - 1:0] wdata, output [WIDTH - 1:0] rdata);
parameter WIDTH = 32;
reg [WIDTH - 1:0] data;
always @(posedge clk) begin
if (wen) begin
data <= wdata;
//$display("writing %d to register", wdata);
end
//$display("on clock data = %d", data);
end
assign rdata = data;
endmodule // register
module coreir_reg(input clk,
input rst,
input en,
input [WIDTH - 1 : 0] in,
output [WIDTH - 1 : 0] out);
parameter WIDTH = 32;
parameter RESET_VALUE = 0;
reg [31:0] data;
always @(posedge clk) begin
if (rst) begin
data <= RESET_VALUE;
end else begin
if (en) begin
data <= in;
end
end
end
assign out = data;
endmodule
module reg_passthrough(input clk, input rst, input [WIDTH - 1:0] raddr, input [WIDTH - 1:0] waddr, input wen, input ren, input [WIDTH - 1:0] wdata, output [WIDTH - 1:0] rdata);
parameter WIDTH = 32;
reg [31:0] data;
always @(posedge clk) begin
if (wen) begin
data <= wdata;
//$display("writing %d to register", wdata);
end
//$display("on clock data = %d", data);
end
assign rdata = wen ? wdata : data;
endmodule
module fifo(input clk,
input rst,
input read_valid,
output read_ready,
input write_valid,
output write_ready,
input [WIDTH - 1 : 0] in_data,
output [WIDTH - 1 : 0] out_data);
parameter WIDTH = 32;
parameter DEPTH = 16;
reg [WIDTH - 1 : 0] ram [DEPTH - 1 : 0];
reg empty;
reg [$clog2(DEPTH) - 1 : 0] write_addr;
reg [$clog2(DEPTH) - 1 : 0] read_addr;
wire [$clog2(DEPTH) - 1 : 0] next_read_addr;
wire [$clog2(DEPTH) - 1 : 0] next_write_addr;
always @(posedge clk) begin
if (!rst) begin
if (write_valid && write_ready) begin
//$display("writing %d to address %d", in_data, write_addr);
// $display("write_addr = %b, next_write_addr = %b, depth = %b", write_addr, next_write_addr, DEPTH);
`assert(write_ready, 1'd1)
ram[write_addr] <= in_data;
write_addr <= next_write_addr;
empty <= 0;
end
end
end
assign next_read_addr = (DEPTH == (read_addr + 1)) ? 0 : read_addr + 1;
assign next_write_addr = (DEPTH == (write_addr + 1)) ? 0 : write_addr + 1;
// always @(*) begin
// $display("READ data is %d, at address 0", ram[0]);
// end
always @(posedge clk) begin
`assert(read_ready && read_valid && write_ready && write_valid, 1'd0)
end
always @(posedge clk) begin
if (!rst) begin
if (read_valid) begin
`assert(read_ready, 1'd1)
// Wraparound
read_addr <= next_read_addr;
if (!empty && (next_read_addr == write_addr) && !write_valid) begin
// $display("FIFO empty: next_read_addr = %d, write_addr = %d", next_read_addr, write_addr);
empty <= 1;
end
end
end
end
reg [WIDTH - 1 : 0] out_data_reg;
always @(posedge clk) begin
if (read_valid && read_ready) begin
//$display("reading from address %d", read_addr);
//$display("reading %d, from address %d", ram[read_addr], read_addr);
out_data_reg <= ram[read_addr];
end
end
assign out_data = out_data_reg;
assign full = !empty && (write_addr == read_addr);
assign write_ready = !full;
// always @(posedge clk) begin
// $display("empty = %d", empty);
// $display("full = %d", full);
// end
assign read_ready = !empty;
always @(posedge clk) begin
if (rst) begin
empty <= 1;
// $display("reseting");
write_addr <= 0;
read_addr <= 0;
end
end
endmodule
module phi( last_block, s, in, out);
parameter integer WIDTH = 32;
parameter integer NB_PAIR = 2;
// last block storage variable is always 32 bits
input [31:0] last_block;
input [32*NB_PAIR-1:0] s;
input [WIDTH*NB_PAIR-1:0] in;
output [WIDTH-1:0] out;
reg [WIDTH-1:0] out_reg;
integer i;
integer found;
always @(*) begin
found = 0;
for (i = 0 ; i < NB_PAIR; i=i+1) begin
if (last_block == s[32*i +: 32]) begin
out_reg = in[i*WIDTH +: WIDTH];
found = 1;
end
end
if (found == 0) begin
$display("Error: last_block: %b not in s : %b",last_block, s);
out_reg = {{WIDTH}{1'bx}};
end
end
assign out = out_reg;
endmodule
module adder(
input_a,
input_b,
input_a_stb,
input_b_stb,
output_z_ack,
clk,
rst,
output_z,
output_z_stb,
input_a_ack,
input_b_ack);
input clk;
input rst;
input [31:0] input_a;
input input_a_stb;
output input_a_ack;
input [31:0] input_b;
input input_b_stb;
output input_b_ack;
output [31:0] output_z;
output output_z_stb;
input output_z_ack;
reg s_output_z_stb;
reg [31:0] s_output_z;
reg s_input_a_ack;
reg s_input_b_ack;
reg [3:0] state;
parameter get_a = 4'd0,
get_b = 4'd1,
unpack = 4'd2,
special_cases = 4'd3,
align = 4'd4,
add_0 = 4'd5,
add_1 = 4'd6,
normalise_1 = 4'd7,
normalise_2 = 4'd8,
round = 4'd9,
pack = 4'd10,
put_z = 4'd11;
reg [31:0] a, b, z;
reg [26:0] a_m, b_m;
reg [23:0] z_m;
reg [9:0] a_e, b_e, z_e;
reg a_s, b_s, z_s;
reg guard, round_bit, sticky;
reg [27:0] sum;
always @(posedge clk) begin
// $display("state = %d", state);
// $display("input_a_stb = %d", input_a_stb);
// $display("input_a_ack = %d", input_a_ack);
// $display("input_b_stb = %d", input_b_stb);
// $display("input_b_ack = %d", input_b_ack);
// $display("output_z_stb = %d", output_z_stb);
// $display("output_z = %d", output_z);
// $display("rst = %d", rst);
end
always @(posedge clk)
begin
case(state)
get_a:
begin
s_input_a_ack <= 1;
if (s_input_a_ack && input_a_stb) begin
a <= input_a;
s_input_a_ack <= 0;
state <= get_b;
end
end
get_b:
begin
s_input_b_ack <= 1;
if (s_input_b_ack && input_b_stb) begin
b <= input_b;
s_input_b_ack <= 0;
state <= unpack;
end
end
unpack:
begin
a_m <= {a[22 : 0], 3'd0};
b_m <= {b[22 : 0], 3'd0};
a_e <= a[30 : 23] - 127;
b_e <= b[30 : 23] - 127;
a_s <= a[31];
b_s <= b[31];
state <= special_cases;
end
special_cases:
begin
//if a is NaN or b is NaN return NaN
if ((a_e == 128 && a_m != 0) || (b_e == 128 && b_m != 0)) begin
z[31] <= 1;
z[30:23] <= 255;
z[22] <= 1;
z[21:0] <= 0;
state <= put_z;
//if a is inf return inf
end else if (a_e == 128) begin
z[31] <= a_s;
z[30:23] <= 255;
z[22:0] <= 0;
//if a is inf and signs don't match return nan
if ((b_e == 128) && (a_s != b_s)) begin
z[31] <= b_s;
z[30:23] <= 255;
z[22] <= 1;
z[21:0] <= 0;
end
state <= put_z;
//if b is inf return inf
end else if (b_e == 128) begin
z[31] <= b_s;
z[30:23] <= 255;
z[22:0] <= 0;
state <= put_z;
//if a is zero return b
end else if ((($signed(a_e) == -127) && (a_m == 0)) && (($signed(b_e) == -127) && (b_m == 0))) begin
z[31] <= a_s & b_s;
z[30:23] <= b_e[7:0] + 127;
z[22:0] <= b_m[26:3];
state <= put_z;
//if a is zero return b
end else if (($signed(a_e) == -127) && (a_m == 0)) begin
z[31] <= b_s;
z[30:23] <= b_e[7:0] + 127;
z[22:0] <= b_m[26:3];
state <= put_z;
//if b is zero return a
end else if (($signed(b_e) == -127) && (b_m == 0)) begin
z[31] <= a_s;
z[30:23] <= a_e[7:0] + 127;
z[22:0] <= a_m[26:3];
state <= put_z;
end else begin
//Denormalised Number
if ($signed(a_e) == -127) begin
a_e <= -126;
end else begin
a_m[26] <= 1;
end
//Denormalised Number
if ($signed(b_e) == -127) begin
b_e <= -126;
end else begin
b_m[26] <= 1;
end
state <= align;
end
end
align:
begin
if ($signed(a_e) > $signed(b_e)) begin
b_e <= b_e + 1;
b_m <= b_m >> 1;
b_m[0] <= b_m[0] | b_m[1];
end else if ($signed(a_e) < $signed(b_e)) begin
a_e <= a_e + 1;
a_m <= a_m >> 1;
a_m[0] <= a_m[0] | a_m[1];
end else begin
state <= add_0;
end
end
add_0:
begin
z_e <= a_e;
if (a_s == b_s) begin
sum <= a_m + b_m;
z_s <= a_s;
end else begin
if (a_m >= b_m) begin
sum <= a_m - b_m;
z_s <= a_s;
end else begin
sum <= b_m - a_m;
z_s <= b_s;
end
end
state <= add_1;
end
add_1:
begin
if (sum[27]) begin
z_m <= sum[27:4];
guard <= sum[3];
round_bit <= sum[2];
sticky <= sum[1] | sum[0];
z_e <= z_e + 1;
end else begin
z_m <= sum[26:3];
guard <= sum[2];
round_bit <= sum[1];
sticky <= sum[0];
end
state <= normalise_1;
end
normalise_1:
begin
if (z_m[23] == 0 && $signed(z_e) > -126) begin
z_e <= z_e - 1;
z_m <= z_m << 1;
z_m[0] <= guard;
guard <= round_bit;
round_bit <= 0;
end else begin
state <= normalise_2;
end
end
normalise_2:
begin
if ($signed(z_e) < -126) begin
z_e <= z_e + 1;
z_m <= z_m >> 1;
guard <= z_m[0];
round_bit <= guard;
sticky <= sticky | round_bit;
end else begin
state <= round;
end
end
round:
begin
if (guard && (round_bit | sticky | z_m[0])) begin
z_m <= z_m + 1;
if (z_m == 24'hffffff) begin
z_e <=z_e + 1;
end
end
state <= pack;
end
pack:
begin
z[22 : 0] <= z_m[22:0];
z[30 : 23] <= z_e[7:0] + 127;
z[31] <= z_s;
if ($signed(z_e) == -126 && z_m[23] == 0) begin
z[30 : 23] <= 0;
end
//if overflow occurs, return inf
if ($signed(z_e) > 127) begin
z[22 : 0] <= 0;
z[30 : 23] <= 255;
z[31] <= z_s;
end
state <= put_z;
end
put_z:
begin
s_output_z_stb <= 1;
s_output_z <= z;
if (s_output_z_stb && output_z_ack) begin
s_output_z_stb <= 0;
state <= get_a;
end
end
endcase
if (rst == 1) begin
state <= get_a;
s_input_a_ack <= 0;
s_input_b_ack <= 0;
s_output_z_stb <= 0;
end
end
assign input_a_ack = s_input_a_ack;
assign input_b_ack = s_input_b_ack;
assign output_z_stb = s_output_z_stb;
assign output_z = s_output_z;
endmodule
module fadd(
input clk,
input en,
input [31 : 0] in0,
input [31 : 0] in1,
output [31 : 0] out);
reg a_stb;
reg b_stb;
wire a_ack;
wire b_ack;
reg out_ack;
wire out_stb;
reg [1:0] state;
parameter setting_a = 2'd0;
parameter setting_b = 2'd1;
parameter waiting = 2'd2;
reg [31 : 0] in0_stored;
reg [31 : 0] in1_stored;
reg [31 : 0] in0_r;
reg [31 : 0] in1_r;
always @(posedge clk) begin
if (en) begin
in0_stored = in0;
in1_stored = in1;
end
end
always @(posedge clk) begin
// $display("ST = %b", state);
// $display("out = %b", out);
// $display("a_ack = %b", a_ack);
// $display("b_ack = %b", b_ack);
// $display("in0 = %b", in0);
// $display("in1 = %b", in1);
// $display("in0_s = %b", in0_stored);
// $display("in1_s = %b", in1_stored);
end
always @(posedge clk) begin
if (en) begin
state <= setting_a;
a_stb <= 1;
b_stb <= 0;
out_ack <= 0;
end else begin
if (out_stb == 1) begin
state <= setting_a;
out_ack <= 1;
end else if (a_ack) begin
b_stb <= 1;
state <= setting_b;
out_ack <= 0;
end else if (b_ack) begin
state <= waiting;