3signal.v 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * 3signal.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2025 Krzysztof Skrzynecki, Jakub Duchniewicz <j.duchniewicz@gmail.com>
  7. * SPDX-License-Identifier: TODO:
  8. */
  9. `default_nettype none
  10. module compare_eq#(
  11. parameter WIDTH = 16
  12. )(
  13. input wire clk,
  14. input wire [WIDTH-1:0] A,
  15. input wire [WIDTH-1:0] B,
  16. output reg [0:0] was_eq //1 if A and B were equal 2 ticks ago
  17. );
  18. parameter WIDTH_DIV4 = (WIDTH+3)/4;
  19. parameter WIDTH_REM = WIDTH - WIDTH_DIV4*3;
  20. wire [WIDTH_DIV4-1:0] A0_pt0, A0_pt1, A0_pt2;
  21. wire [WIDTH_REM-1:0] A0_pt3;
  22. wire [WIDTH_DIV4-1:0] B0_pt0, B0_pt1, B0_pt2;
  23. wire [WIDTH_REM-1:0] B0_pt3;
  24. assign {A0_pt3, A0_pt2, A0_pt1, A0_pt0} = A;
  25. assign {B0_pt3, B0_pt2, B0_pt1, B0_pt0} = B;
  26. reg pt0_eq, pt1_eq, pt2_eq, pt3_eq;
  27. always @(posedge clk) begin
  28. pt0_eq <= A0_pt0==B0_pt0;
  29. pt1_eq <= A0_pt1==B0_pt1;
  30. pt2_eq <= A0_pt2==B0_pt2;
  31. pt3_eq <= A0_pt3==B0_pt3;
  32. was_eq <= pt0_eq & pt1_eq & pt2_eq & pt3_eq;
  33. end
  34. endmodule
  35. module add#(
  36. parameter WIDTH = 16
  37. )(
  38. input wire clk,
  39. input wire [WIDTH-1:0] A,
  40. input wire [WIDTH-1:0] B,
  41. output reg [WIDTH-1:0] sum
  42. );
  43. parameter WIDTH_DIV2 = (WIDTH+1)/2;
  44. parameter WIDTH_REM = WIDTH - WIDTH_DIV2;
  45. wire [WIDTH_DIV2-1:0] A0_pt0;
  46. wire [WIDTH_REM-1:0] A0_pt1;
  47. wire [WIDTH_DIV2-1:0] B0_pt0;
  48. wire [WIDTH_REM-1:0] B0_pt1;
  49. assign {A0_pt1, A0_pt0} = A;
  50. assign {B0_pt1, B0_pt0} = B;
  51. reg [WIDTH_DIV2:0] C0_pt0;
  52. reg [WIDTH_REM-1:0] C0_pt1;
  53. always @(posedge clk) begin
  54. C0_pt0 <= {1'b0, A0_pt0} + {1'b0, B0_pt0};
  55. C0_pt1 <= A0_pt1 + B0_pt1;
  56. sum <= {C0_pt1+{6'b0, C0_pt0[WIDTH_DIV2]}, C0_pt0[WIDTH_DIV2-1:0]};
  57. end
  58. endmodule
  59. module compare_gt#(
  60. parameter WIDTH = 16
  61. )(
  62. input wire clk,
  63. input wire [WIDTH-1:0] A,
  64. input wire [WIDTH-1:0] B,
  65. output reg [0:0] was_gt //1 if A was greater than B 3 ticks ago
  66. );
  67. parameter WIDTH_DIV4 = (WIDTH+3)/4;
  68. parameter WIDTH_REM = WIDTH - WIDTH_DIV4*3;
  69. wire [WIDTH_DIV4-1:0] A0_pt0, A0_pt1, A0_pt2;
  70. wire [WIDTH_REM-1:0] A0_pt3;
  71. wire [WIDTH_DIV4-1:0] B0_pt0, B0_pt1, B0_pt2;
  72. wire [WIDTH_REM-1:0] B0_pt3;
  73. assign {A0_pt3, A0_pt2, A0_pt1, A0_pt0} = A;
  74. assign {B0_pt3, B0_pt2, B0_pt1, B0_pt0} = B;
  75. reg pt0_gt, pt1_gt, pt2_gt, pt3_gt;
  76. reg pt0_eq, pt1_eq, pt2_eq, pt3_eq;
  77. reg pt01_eq, pt23_eq;
  78. reg pt01_gt, pt23_gt;
  79. always @(posedge clk) begin
  80. pt0_gt <= A0_pt0>B0_pt0;
  81. pt1_gt <= A0_pt1>B0_pt1;
  82. pt2_gt <= A0_pt2>B0_pt2;
  83. pt3_gt <= A0_pt3>B0_pt3;
  84. pt0_eq <= A0_pt0==B0_pt0;
  85. pt1_eq <= A0_pt1==B0_pt1;
  86. pt2_eq <= A0_pt2==B0_pt2;
  87. pt3_eq <= A0_pt3==B0_pt3;
  88. pt01_eq <= pt0_eq & pt1_eq;
  89. pt01_gt <= pt1_gt | (pt1_eq & pt0_gt);
  90. pt23_eq <= pt2_eq & pt3_eq;
  91. pt23_gt <= pt3_gt | (pt3_eq & pt2_gt);
  92. was_gt <= pt23_gt | (pt23_eq & pt01_gt);
  93. end
  94. endmodule
  95. module cycle_trigger#(
  96. parameter WIDTH = 16
  97. ) (
  98. input wire /*nrst,*/ clk,
  99. input wire [WIDTH-1:0] period,
  100. output reg [0:0] start_cycle,
  101. output reg [0:0] half_cycle,
  102. output reg [2:0] trig_out
  103. );
  104. reg [WIDTH-1:0] counter;
  105. reg [WIDTH-1:0] counter_delayed;// just to make placement easier - less branches from counter
  106. reg [WIDTH-1:0] period_div2;
  107. reg [WIDTH-1:0] period_shadow;
  108. wire zero;
  109. always @(posedge clk /*or negedge nrst*/) begin
  110. /*if (!nrst) begin
  111. counter <= 0;
  112. //period_div2 <= 16'h0;
  113. high_match_a <= 0;
  114. high_match_b <= 0;
  115. half_match_a <= 0;
  116. half_match_b <= 0;
  117. start_cycle <= 0;
  118. half_cycle <= 0;
  119. trig_out <= 0;
  120. end else begin*/
  121. if(zero) begin
  122. start_cycle <= 1;
  123. period_div2 <= period>>1;
  124. period_shadow <= period;
  125. end else begin
  126. start_cycle <= 0;
  127. end
  128. if (start_cycle) begin
  129. counter <= period_shadow;
  130. end else begin
  131. counter <= counter-1;
  132. end
  133. counter_delayed <= counter;
  134. //end //nrst
  135. end
  136. compare_eq#(.WIDTH(WIDTH))cmp_zero(
  137. .clk(clk),
  138. .A(counter_delayed),
  139. .B(0),
  140. .was_eq(zero)
  141. );
  142. compare_eq#(.WIDTH(WIDTH))cmp_half(
  143. .clk(clk),
  144. .A(counter_delayed),
  145. .B(period_div2),
  146. .was_eq(half_cycle)
  147. );
  148. compare_eq#(.WIDTH(WIDTH))cmp0(
  149. .clk(clk),
  150. .A(counter_delayed),
  151. .B(3),
  152. .was_eq(trig_out[0])
  153. );
  154. compare_eq#(.WIDTH(WIDTH))cmp1(
  155. .clk(clk),
  156. .A(counter_delayed),
  157. .B(3),
  158. .was_eq(trig_out[1])
  159. );
  160. compare_eq#(.WIDTH(WIDTH))cmp2(
  161. .clk(clk),
  162. .A(counter_delayed),
  163. .B(9),
  164. .was_eq(trig_out[2])
  165. );
  166. endmodule
  167. /*module continous_pwm_gen#(
  168. parameter WIDTH = 16
  169. ) (
  170. input wire nrst, clk,
  171. input wire [WIDTH-1:0] period,
  172. input wire [WIDTH-1:0] delay,
  173. output reg [0:0] pwm_out
  174. );
  175. reg [WIDTH-1:0] counter;
  176. reg [WIDTH-1:0] _period;
  177. reg [WIDTH-1:0] _delay;
  178. reg [0:0] next_out;
  179. reg [0:0] is_last_tick;
  180. reg [0:0] is_last_tickA;
  181. reg [0:0] is_last_tickB;
  182. reg [0:0] is_mid_tick;
  183. // Counter logic
  184. always @(posedge clk or negedge nrst) begin
  185. if (!nrst) begin
  186. counter <= 0;
  187. pwm_out <= 0;
  188. next_out <= 0;
  189. _period <= 0;
  190. _delay <= 0;
  191. is_last_tick <= 0;
  192. is_last_tickA <= 0;
  193. is_last_tickB <= 0;
  194. end else begin
  195. _period <= period;
  196. _delay <= delay;
  197. is_last_tickA <= counter[WIDTH-1:8]==0;
  198. is_last_tickB <= counter[7:0]==2;
  199. is_last_tick <= is_last_tickA&is_last_tickB;
  200. if (is_last_tick) begin
  201. counter <= _period;
  202. end else begin
  203. counter <= counter-1;
  204. end
  205. next_out <= is_last_tick;
  206. pwm_out <= next_out;
  207. end
  208. end
  209. endmodule*/
  210. module single_shot_gen#(
  211. parameter WIDTH = 16
  212. )(
  213. input wire nrst, clk,
  214. input wire trigger,
  215. input [WIDTH-1:0] delay,
  216. input [WIDTH-1:0] period,
  217. output reg [0:0] pwm_out
  218. );
  219. reg [WIDTH:0] counter;
  220. reg [WIDTH:0] counter2;
  221. wire pwm_gt;
  222. //reg [0:0] is_counting;
  223. //reg [WIDTH-1:0] next_cnt;
  224. //reg [WIDTH-1:0] period;
  225. //wire [WIDTH-1:0] last_avail_cnt=period-1;
  226. always @(posedge clk /*or negedge nrst*/) begin
  227. if (trigger) begin
  228. counter <= {1'b0,period};
  229. end else begin
  230. counter <= counter-1;
  231. end
  232. counter2 <= counter;
  233. pwm_out <= pwm_gt & !counter2[WIDTH];
  234. end
  235. compare_gt#(.WIDTH(WIDTH))cmp_out(
  236. .clk(clk),
  237. .A(delay),
  238. .B(counter2[WIDTH-1:0]),
  239. .was_gt(pwm_gt)
  240. );
  241. endmodule
  242. /*module phase_delay#(
  243. parameter WIDTH = 16,
  244. parameter FORWARD_DATA_WIDTH = 16
  245. )(
  246. input wire nrst, clk,
  247. input wire in_trig,
  248. input [WIDTH-1:0] delay,
  249. input [FORWARD_DATA_WIDTH-1:0] in_data_fwd,
  250. output wire out_trig,
  251. output reg [FORWARD_DATA_WIDTH-1:0] out_data_fwd
  252. );
  253. reg [WIDTH-1:0] counter;
  254. reg [0:0] is_counting;
  255. reg [WIDTH-1:0] delay_latched;
  256. always @(posedge clk or negedge nrst) begin
  257. if (!nrst) begin
  258. counter <= 0;
  259. is_counting <=0;
  260. delay_latched <=0;
  261. end else begin
  262. if (is_counting) begin
  263. if ((counter+1) >= delay_latched) begin
  264. counter <= 0;
  265. is_counting <= 0;
  266. end else begin
  267. counter <= counter+1;
  268. end
  269. end else begin
  270. if (in_trig) begin
  271. is_counting <= 1;
  272. out_data_fwd <= in_data_fwd;
  273. delay_latched <= delay;
  274. end
  275. end
  276. end
  277. end
  278. assign out_trig=((counter+1)>=delay) && is_counting;
  279. endmodule*/
  280. module simple_counter#(
  281. parameter WIDTH = 14
  282. )(
  283. input wire clk,
  284. input wire trigger,
  285. input wire if_counting,
  286. output reg [WIDTH-1:0] cnt_out
  287. );
  288. parameter HALF_WIDTH=7;
  289. reg [HALF_WIDTH-1:0] cnt1, cnt0;
  290. reg cnt0f;
  291. always @(posedge clk /*or negedge nrst*/) begin
  292. if (trigger) begin
  293. cnt1 <= 0;
  294. cnt0 <= 0;
  295. cnt0f <= 0;
  296. end else begin
  297. //cnt1f <= &cnt1;
  298. cnt0f <= &cnt0;
  299. if(if_counting) begin
  300. cnt0 <= cnt0+1;
  301. if(cnt0f) begin
  302. cnt1 <= cnt1+1;
  303. end
  304. end
  305. cnt_out <= {cnt1, cnt0};
  306. end
  307. end
  308. endmodule
  309. module pulse_train_gen#(
  310. parameter TOTAL_PERIOD_WIDTH = 14,
  311. parameter SINGLE_CYCLE_WIDTH = 8,
  312. parameter PULSE_COUNTER_WIDTH = 8
  313. )(
  314. input wire nrst, clk,
  315. input wire trigger,
  316. input [PULSE_COUNTER_WIDTH-1:0] npuls,
  317. input [SINGLE_CYCLE_WIDTH-1:0] period,
  318. input [SINGLE_CYCLE_WIDTH-1:0] duty,
  319. output reg [0:0] pwm_out
  320. );
  321. reg [TOTAL_PERIOD_WIDTH-1:0] counter;//total waveform counter
  322. reg [TOTAL_PERIOD_WIDTH-1:0] counter2;
  323. reg [TOTAL_PERIOD_WIDTH-1:0] cycle_threshold;
  324. reg [TOTAL_PERIOD_WIDTH-1:0] cycle_threshold2;
  325. reg [TOTAL_PERIOD_WIDTH-1:0] next_cycle_threshold;
  326. reg [PULSE_COUNTER_WIDTH-1:0] cycle_counter;
  327. reg [PULSE_COUNTER_WIDTH-1:0] next_cycle_counter;
  328. reg [SINGLE_CYCLE_WIDTH-1:0] _period;
  329. reg if_next_cycle;
  330. reg is_counting;
  331. reg is_counting2;
  332. always @(posedge clk /*or negedge nrst*/) begin
  333. /*if (!nrst) begin
  334. is_counting <= 0;
  335. end else begin*/
  336. if(trigger) begin
  337. //counter <= 0;
  338. cycle_threshold <= {6'b0, _period};
  339. cycle_counter <= npuls;
  340. end else begin
  341. //counter <= counter + is_counting2;
  342. end
  343. counter2 <= counter;
  344. _period <= period;
  345. if(if_next_cycle & is_counting2) begin
  346. cycle_counter <= next_cycle_counter;
  347. cycle_threshold <= next_cycle_threshold;
  348. end
  349. next_cycle_counter <= cycle_counter-1;
  350. is_counting <= |next_cycle_counter;
  351. is_counting2 <= is_counting;
  352. pwm_out <= if_next_cycle;
  353. cycle_threshold2 <= cycle_threshold;
  354. //end
  355. end
  356. simple_counter cnt(
  357. .clk(clk),
  358. .trigger(trigger),
  359. .if_counting(is_counting2),
  360. .cnt_out(counter)
  361. );
  362. compare_eq#(.WIDTH(TOTAL_PERIOD_WIDTH))cmp_cycle(
  363. .clk(clk),
  364. .A(counter2),
  365. .B(cycle_threshold2),
  366. .was_eq(if_next_cycle)
  367. );
  368. /*add#(
  369. .WIDTH(TOTAL_PERIOD_WIDTH)
  370. )cycle_th(
  371. .clk(clk),
  372. .A(cycle_threshold2),
  373. .B({6'b0, _period}),
  374. .sum(next_cycle_threshold)
  375. );*/
  376. endmodule
  377. parameter [1:0] ODD_TRAIN_FORCE_OFF = 2'b00;
  378. parameter [1:0] ODD_TRAIN_ENA_CONTROL = 2'b01;
  379. parameter [1:0] ODD_TRAIN_FORCE_ON = 2'b10;
  380. module three_signal#(
  381. parameter FAST_PWM_WIDTH=8,
  382. parameter PULSE_COUNTER_WIDTH=8,
  383. parameter SLOW_PWM_WIDTH=14
  384. )(
  385. input wire nrst, clk,
  386. input [SLOW_PWM_WIDTH-1:0] period1,
  387. input [SLOW_PWM_WIDTH-1:0] delay1,
  388. input [SLOW_PWM_WIDTH-1:0] period2,
  389. input [SLOW_PWM_WIDTH-1:0] delay2,
  390. input [FAST_PWM_WIDTH-1:0] period3,
  391. input [FAST_PWM_WIDTH-1:0] duty3,
  392. input [SLOW_PWM_WIDTH-1:0] delay3,//delay is wrt slow pwm, thus longer bit length
  393. input [PULSE_COUNTER_WIDTH-1:0] npuls3,
  394. input /*odd_train_flag_t*/ wire [1:0] odd_train_flag,
  395. input wire ena_odd_out3,
  396. output reg Out1,
  397. output reg Out2,
  398. output reg Out3
  399. );
  400. reg [SLOW_PWM_WIDTH-1:0] _period1;
  401. reg [SLOW_PWM_WIDTH-1:0] _delay1;
  402. reg [SLOW_PWM_WIDTH-1:0] _duty1;
  403. reg [SLOW_PWM_WIDTH-1:0] _period2;
  404. reg [SLOW_PWM_WIDTH-1:0] _delay2;
  405. reg [FAST_PWM_WIDTH-1:0] _period3;
  406. reg [FAST_PWM_WIDTH-1:0] _duty3;
  407. reg [SLOW_PWM_WIDTH-1:0] _delay3;//delay is wrt slow pwm, thus longer bit length
  408. reg [PULSE_COUNTER_WIDTH-1:0] _npuls3;
  409. reg /*odd_train_flag_t*/ [1:0] _odd_train_flag;
  410. reg _ena_odd_out3;
  411. wire _Out1;
  412. wire _Out2;
  413. wire _Out3;
  414. always @(posedge clk) begin
  415. _period1 <= period1 ;
  416. _delay1 <= delay1 ;
  417. _period2 <= period2 ;
  418. _delay2 <= delay2 ;
  419. _period3 <= period3 ;
  420. _duty3 <= duty3 ;
  421. _delay3 <= delay3 ;
  422. _npuls3 <= npuls3 ;
  423. _odd_train_flag <= odd_train_flag;
  424. _ena_odd_out3 <= ena_odd_out3 ;
  425. //TODO - output already as reg; no additional latency needed?
  426. Out1 <= _Out1;
  427. Out2 <= _Out2;
  428. Out3 <= _Out3;
  429. end
  430. //wire trigger_next_cycle;
  431. //wire trigger_even_cycle;
  432. //wire trigger_odd_cycle;
  433. wire [SLOW_PWM_WIDTH-1:0] delay3_part1;
  434. wire [SLOW_PWM_WIDTH-1:0] delay3_part234;
  435. assign delay3_part234 = delay3>>2;
  436. assign delay3_part1 = (delay3<4) ? 0 : (delay3 - (delay3_part234*3));
  437. wire [0:0] start_cycle;
  438. wire [0:0] half_cycle;
  439. wire [2:0] trig_out;
  440. cycle_trigger #(.WIDTH(SLOW_PWM_WIDTH)) cyc_trig(
  441. //.nrst(nrst),
  442. .clk(clk),
  443. .period(period1),
  444. .start_cycle(start_cycle),
  445. .half_cycle(half_cycle),
  446. .trig_out(trig_out)
  447. );
  448. single_shot_gen #(.WIDTH(SLOW_PWM_WIDTH)) pwm1(
  449. .nrst(nrst),
  450. .clk(clk),
  451. .trigger(trig_out[0]),
  452. .delay(_delay1),
  453. .period(_period1),
  454. .pwm_out(_Out1)
  455. );
  456. single_shot_gen #(.WIDTH(SLOW_PWM_WIDTH)) pwm2(
  457. .nrst(nrst),
  458. .clk(clk),
  459. .trigger(trig_out[1]),
  460. .delay(_delay2),
  461. .period(_period2),
  462. .pwm_out(_Out2)
  463. );
  464. pulse_train_gen#(
  465. .TOTAL_PERIOD_WIDTH(SLOW_PWM_WIDTH),
  466. .SINGLE_CYCLE_WIDTH(FAST_PWM_WIDTH),
  467. .PULSE_COUNTER_WIDTH(PULSE_COUNTER_WIDTH)
  468. )pwm3(
  469. .nrst(nrst),
  470. .clk(clk),
  471. .trigger(trig_out[2]),
  472. .npuls(_npuls3),
  473. .period(_period3),
  474. .duty(_duty3),
  475. .pwm_out(_Out3)
  476. );
  477. /*wire trigger_delay_1_to_2;
  478. wire trigger_delay_2_to_3;
  479. wire trigger_delay_3_to_4;
  480. wire trigger_delay_4_to_pulse_train;
  481. localparam FORWARD_DATA_WIDTH = SLOW_PWM_WIDTH + PULSE_COUNTER_WIDTH + FAST_PWM_WIDTH + FAST_PWM_WIDTH;
  482. localparam DELAY_BITS_POS = PULSE_COUNTER_WIDTH + FAST_PWM_WIDTH + FAST_PWM_WIDTH;
  483. wire [FORWARD_DATA_WIDTH-1:0] data_1_to_2;
  484. wire [FORWARD_DATA_WIDTH-1:0] data_2_to_3;
  485. wire [FORWARD_DATA_WIDTH-1:0] data_3_to_4;
  486. wire [FORWARD_DATA_WIDTH-1:0] data_4_to_gen;
  487. assign trigger_odd_cycle =
  488. (odd_train_flag == ODD_TRAIN_ENA_CONTROL) ? trigger_next_cycle && ena_odd_out3 :
  489. (odd_train_flag == ODD_TRAIN_FORCE_ON) ? trigger_next_cycle :
  490. 0;
  491. wire [FAST_PWM_WIDTH-1:0] period3_gen;
  492. wire [FAST_PWM_WIDTH-1:0] duty3_gen;
  493. wire [PULSE_COUNTER_WIDTH-1:0] npuls3_gen;
  494. assign duty3_gen = data_4_to_gen[FAST_PWM_WIDTH-1:0];
  495. assign period3_gen = data_4_to_gen[FAST_PWM_WIDTH+FAST_PWM_WIDTH-1:FAST_PWM_WIDTH];
  496. assign npuls3_gen = data_4_to_gen[FAST_PWM_WIDTH+FAST_PWM_WIDTH+PULSE_COUNTER_WIDTH-1:FAST_PWM_WIDTH+FAST_PWM_WIDTH];*/
  497. endmodule