data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v
None: at end of input
1: b'%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:45: Cannot find include file: omsp_timerA_defines.v\n`include "omsp_timerA_defines.v" \n         ^~~~~~~~~~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/152581089/Verilog/MSP430,data/full_repos/permissive/152581089/omsp_timerA_defines.v\n             data/full_repos/permissive/152581089/Verilog/MSP430,data/full_repos/permissive/152581089/omsp_timerA_defines.v.v\n             data/full_repos/permissive/152581089/Verilog/MSP430,data/full_repos/permissive/152581089/omsp_timerA_defines.v.sv\n             omsp_timerA_defines.v\n             omsp_timerA_defines.v.v\n             omsp_timerA_defines.v.sv\n             obj_dir/omsp_timerA_defines.v\n             obj_dir/omsp_timerA_defines.v.v\n             obj_dir/omsp_timerA_defines.v.sv\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:758: Cannot find include file: omsp_timerA_undefines.v\n`include "omsp_timerA_undefines.v" \n         ^~~~~~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:192: Define or directive not defined: \'`TACLR\'\nwire        taclr    = tactl_wr & per_din[`TACLR];\n                                          ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:192: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nwire        taclr    = tactl_wr & per_din[`TACLR];\n                                                ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:321: Define or directive not defined: \'`TACCIFG\'\nwire [3:0] taiv = (tacctl1[`TACCIFG] & tacctl1[`TACCIE]) ? 4\'h2 : \n                           ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:321: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nwire [3:0] taiv = (tacctl1[`TACCIFG] & tacctl1[`TACCIE]) ? 4\'h2 : \n                                   ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:321: Define or directive not defined: \'`TACCIE\'\nwire [3:0] taiv = (tacctl1[`TACCIFG] & tacctl1[`TACCIE]) ? 4\'h2 : \n                                               ^~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:322: Define or directive not defined: \'`TACCIFG\'\n                  (tacctl2[`TACCIFG] & tacctl2[`TACCIE]) ? 4\'h4 : \n                           ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:322: Define or directive not defined: \'`TACCIE\'\n                  (tacctl2[`TACCIFG] & tacctl2[`TACCIE]) ? 4\'h4 : \n                                               ^~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:323: Define or directive not defined: \'`TAIFG\'\n                  (tactl[`TAIFG]     & tactl[`TAIE])     ? 4\'hA : \n                         ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:323: Define or directive not defined: \'`TAIE\'\n                  (tactl[`TAIFG]     & tactl[`TAIE])     ? 4\'hA : \n                                             ^~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:405: Define or directive not defined: \'`TASSELx\'\nwire sel_clk = (tactl[`TASSELx]==2\'b00) ? taclk_en :\n                      ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:405: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nwire sel_clk = (tactl[`TASSELx]==2\'b00) ? taclk_en :\n                              ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:406: Define or directive not defined: \'`TASSELx\'\n               (tactl[`TASSELx]==2\'b01) ?  aclk_en :\n                      ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:407: Define or directive not defined: \'`TASSELx\'\n               (tactl[`TASSELx]==2\'b10) ? smclk_en : inclk_en;\n                      ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:414: Define or directive not defined: \'`TAIDx\'\nassign    tar_clk = sel_clk & ((tactl[`TAIDx]==2\'b00) ?  1\'b1         :\n                                      ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:414: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nassign    tar_clk = sel_clk & ((tactl[`TAIDx]==2\'b00) ?  1\'b1         :\n                                            ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:415: Define or directive not defined: \'`TAIDx\'\n                               (tactl[`TAIDx]==2\'b01) ?  clk_div[0]   :\n                                      ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:416: Define or directive not defined: \'`TAIDx\'\n                               (tactl[`TAIDx]==2\'b10) ? &clk_div[1:0] :\n                                      ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:422: Define or directive not defined: \'`TAMCx\'\n  else if ((tactl[`TAMCx]!=2\'b00) & sel_clk) clk_div <=  clk_div+3\'h1;\n                  ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:422: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\n  else if ((tactl[`TAMCx]!=2\'b00) & sel_clk) clk_div <=  clk_div+3\'h1;\n                        ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:428: Define or directive not defined: \'`TAMCx\'\nassign  tar_clr   = ((tactl[`TAMCx]==2\'b01) & (tar>=taccr0))         |\n                            ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:428: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nassign  tar_clr   = ((tactl[`TAMCx]==2\'b01) & (tar>=taccr0))         |\n                                  ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:429: Define or directive not defined: \'`TAMCx\'\n                    ((tactl[`TAMCx]==2\'b11) & (taccr0==16\'h0000));\n                            ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:431: Define or directive not defined: \'`TAMCx\'\nassign  tar_inc   =  (tactl[`TAMCx]==2\'b01) | (tactl[`TAMCx]==2\'b10) | \n                            ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:431: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nassign  tar_inc   =  (tactl[`TAMCx]==2\'b01) | (tactl[`TAMCx]==2\'b10) | \n                                  ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:431: Define or directive not defined: \'`TAMCx\'\nassign  tar_inc   =  (tactl[`TAMCx]==2\'b01) | (tactl[`TAMCx]==2\'b10) | \n                                                     ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:432: Define or directive not defined: \'`TAMCx\'\n                    ((tactl[`TAMCx]==2\'b11) & ~tar_dec);\n                            ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:438: Define or directive not defined: \'`TAMCx\'\n  else if (tactl[`TAMCx]==2\'b11)\n                 ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:438: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\n  else if (tactl[`TAMCx]==2\'b11)\n                       ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:445: Define or directive not defined: \'`TAMCx\'\nassign tar_dec = tar_dir | ((tactl[`TAMCx]==2\'b11) & (tar>=taccr0));\n                                   ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:445: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nassign tar_dec = tar_dir | ((tactl[`TAMCx]==2\'b11) & (tar>=taccr0));\n                                         ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:463: Define or directive not defined: \'`TACCISx\'\nassign cci0 = (tacctl0[`TACCISx]==2\'b00) ? ta_cci0a :\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:463: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nassign cci0 = (tacctl0[`TACCISx]==2\'b00) ? ta_cci0a :\n                               ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:464: Define or directive not defined: \'`TACCISx\'\n              (tacctl0[`TACCISx]==2\'b01) ? ta_cci0b :\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:465: Define or directive not defined: \'`TACCISx\'\n              (tacctl0[`TACCISx]==2\'b10) ?     1\'b0 : 1\'b1;\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:467: Define or directive not defined: \'`TACCISx\'\nassign cci1 = (tacctl1[`TACCISx]==2\'b00) ? ta_cci1a :\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:467: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nassign cci1 = (tacctl1[`TACCISx]==2\'b00) ? ta_cci1a :\n                               ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:468: Define or directive not defined: \'`TACCISx\'\n              (tacctl1[`TACCISx]==2\'b01) ? ta_cci1b :\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:469: Define or directive not defined: \'`TACCISx\'\n              (tacctl1[`TACCISx]==2\'b10) ?     1\'b0 : 1\'b1;\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:471: Define or directive not defined: \'`TACCISx\'\nassign cci2 = (tacctl2[`TACCISx]==2\'b00) ? ta_cci2a :\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:471: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nassign cci2 = (tacctl2[`TACCISx]==2\'b00) ? ta_cci2a :\n                               ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:472: Define or directive not defined: \'`TACCISx\'\n              (tacctl2[`TACCISx]==2\'b01) ? ta_cci2b :\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:473: Define or directive not defined: \'`TACCISx\'\n              (tacctl2[`TACCISx]==2\'b10) ?     1\'b0 : 1\'b1;\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:537: Define or directive not defined: \'`TACMx\'\nwire cci0_evt = (tacctl0[`TACMx]==2\'b00) ? 1\'b0                  :\n                         ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:537: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nwire cci0_evt = (tacctl0[`TACMx]==2\'b00) ? 1\'b0                  :\n                               ^\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:538: Define or directive not defined: \'`TACMx\'\n                (tacctl0[`TACMx]==2\'b01) ? ( cci0_s & ~cci0_dly) :    \n                         ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:539: Define or directive not defined: \'`TACMx\'\n                (tacctl0[`TACMx]==2\'b10) ? (~cci0_s &  cci0_dly) :    \n                         ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:542: Define or directive not defined: \'`TACMx\'\nwire cci1_evt = (tacctl1[`TACMx]==2\'b00) ? 1\'b0                  :\n                         ^~~~~~\n%Error: data/full_repos/permissive/152581089/Verilog/MSP430/omsp_timerA.v:542: syntax error, unexpected \']\', expecting TYPE-IDENTIFIER\nwire cci1_evt = (tacctl1[`TACMx]==2\'b00) ? 1\'b0                  :\n                               ^\n%Error: Exiting due to too many errors encountered; --error-limit=50\n'
module  omsp_timerA (


    irq_ta0,
    irq_ta1,
    per_dout,
    ta_out0,
    ta_out0_en,
    ta_out1,
    ta_out1_en,
    ta_out2,
    ta_out2_en,


    aclk_en,
    dbg_freeze,
    inclk,
    irq_ta0_acc,
    mclk,
    per_addr,
    per_din,
    per_en,
    per_we,
    puc_rst,
    smclk_en,
    ta_cci0a,
    ta_cci0b,
    ta_cci1a,
    ta_cci1b,
    ta_cci2a,
    ta_cci2b,
    taclk
);



output              irq_ta0;
output              irq_ta1;
output       [15:0] per_dout;
output              ta_out0;
output              ta_out0_en;
output              ta_out1;
output              ta_out1_en;
output              ta_out2;
output              ta_out2_en;



input               aclk_en;
input               dbg_freeze;
input               inclk;
input               irq_ta0_acc;
input               mclk;
input        [13:0] per_addr;
input        [15:0] per_din;
input               per_en;
input         [1:0] per_we;
input               puc_rst;
input               smclk_en;
input               ta_cci0a;
input               ta_cci0b;
input               ta_cci1a;
input               ta_cci1b;
input               ta_cci2a;
input               ta_cci2b;
input               taclk;







parameter       [14:0] BASE_ADDR  = 15'h0100;


parameter              DEC_WD     =  7;


parameter [DEC_WD-1:0] TACTL      = 'h60,
                       TAR        = 'h70,
                       TACCTL0    = 'h62,
                       TACCR0     = 'h72,
                       TACCTL1    = 'h64,
                       TACCR1     = 'h74,
                       TACCTL2    = 'h66,
                       TACCR2     = 'h76,
                       TAIV       = 'h2E;


parameter              DEC_SZ     =  (1 << DEC_WD);
parameter [DEC_SZ-1:0] BASE_REG   =  {{DEC_SZ-1{1'b0}}, 1'b1};


parameter [DEC_SZ-1:0] TACTL_D    = (BASE_REG << TACTL),
                       TAR_D      = (BASE_REG << TAR),
                       TACCTL0_D  = (BASE_REG << TACCTL0),
                       TACCR0_D   = (BASE_REG << TACCR0),
                       TACCTL1_D  = (BASE_REG << TACCTL1),
                       TACCR1_D   = (BASE_REG << TACCR1),
                       TACCTL2_D  = (BASE_REG << TACCTL2),
                       TACCR2_D   = (BASE_REG << TACCR2),
                       TAIV_D     = (BASE_REG << TAIV);







wire              reg_sel   =  per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);


wire [DEC_WD-1:0] reg_addr  =  {per_addr[DEC_WD-2:0], 1'b0};


wire [DEC_SZ-1:0] reg_dec   =  (TACTL_D    &  {DEC_SZ{(reg_addr == TACTL   )}})  |
                               (TAR_D      &  {DEC_SZ{(reg_addr == TAR     )}})  |
                               (TACCTL0_D  &  {DEC_SZ{(reg_addr == TACCTL0 )}})  |
                               (TACCR0_D   &  {DEC_SZ{(reg_addr == TACCR0  )}})  |
                               (TACCTL1_D  &  {DEC_SZ{(reg_addr == TACCTL1 )}})  |
                               (TACCR1_D   &  {DEC_SZ{(reg_addr == TACCR1  )}})  |
                               (TACCTL2_D  &  {DEC_SZ{(reg_addr == TACCTL2 )}})  |
                               (TACCR2_D   &  {DEC_SZ{(reg_addr == TACCR2  )}})  |
                               (TAIV_D     &  {DEC_SZ{(reg_addr == TAIV    )}});


wire              reg_write =  |per_we & reg_sel;
wire              reg_read  = ~|per_we & reg_sel;


wire [DEC_SZ-1:0] reg_wr    = reg_dec & {512{reg_write}};
wire [DEC_SZ-1:0] reg_rd    = reg_dec & {512{reg_read}};








reg   [9:0] tactl;

wire        tactl_wr = reg_wr[TACTL];
wire        taclr    = tactl_wr & per_din[`TACLR];
wire        taifg_set;
wire        taifg_clr;
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)       tactl <=  10'h000;
  else if (tactl_wr) tactl <=  ((per_din[9:0] & 10'h3f3) | {9'h000, taifg_set}) & {9'h1ff, ~taifg_clr};
  else               tactl <=  (tactl                    | {9'h000, taifg_set}) & {9'h1ff, ~taifg_clr};




reg  [15:0] tar;

wire        tar_wr = reg_wr[TAR];

wire        tar_clk;
wire        tar_clr;
wire        tar_inc;
wire        tar_dec;
wire [15:0] tar_add  = tar_inc ? 16'h0001 :
                       tar_dec ? 16'hffff : 16'h0000;
wire [15:0] tar_nxt  = tar_clr ? 16'h0000 : (tar+tar_add);
  
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                     tar <=  16'h0000;
  else if  (tar_wr)                tar <=  per_din;
  else if  (taclr)                 tar <=  16'h0000;
  else if  (tar_clk & ~dbg_freeze) tar <=  tar_nxt;




reg  [15:0] tacctl0;

wire        tacctl0_wr = reg_wr[TACCTL0];
wire        ccifg0_set;
wire        cov0_set;   

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)         tacctl0  <=  16'h0000;
  else if (tacctl0_wr) tacctl0  <=  ((per_din & 16'hf9f7) | {14'h0000, cov0_set, ccifg0_set}) & {15'h7fff, ~irq_ta0_acc};
  else                 tacctl0  <=  (tacctl0              | {14'h0000, cov0_set, ccifg0_set}) & {15'h7fff, ~irq_ta0_acc};

wire        cci0;
reg         scci0;
wire [15:0] tacctl0_full = tacctl0 | {5'h00, scci0, 6'h00, cci0, 3'h0};

   


reg  [15:0] taccr0;

wire        taccr0_wr = reg_wr[TACCR0];
wire        cci0_cap;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)        taccr0 <=  16'h0000;
  else if (taccr0_wr) taccr0 <=  per_din;
  else if (cci0_cap)  taccr0 <=  tar;

   


reg  [15:0] tacctl1;

wire        tacctl1_wr = reg_wr[TACCTL1];
wire        ccifg1_set;
wire        ccifg1_clr;
wire        cov1_set;   
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)         tacctl1 <=  16'h0000;
  else if (tacctl1_wr) tacctl1 <=  ((per_din & 16'hf9f7) | {14'h0000, cov1_set, ccifg1_set}) & {15'h7fff, ~ccifg1_clr};
  else                 tacctl1 <=  (tacctl1              | {14'h0000, cov1_set, ccifg1_set}) & {15'h7fff, ~ccifg1_clr};

wire        cci1;
reg         scci1;
wire [15:0] tacctl1_full = tacctl1 | {5'h00, scci1, 6'h00, cci1, 3'h0};

   


reg  [15:0] taccr1;

wire        taccr1_wr = reg_wr[TACCR1];
wire        cci1_cap;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)        taccr1 <=  16'h0000;
  else if (taccr1_wr) taccr1 <=  per_din;
  else if (cci1_cap)  taccr1 <=  tar;




reg  [15:0] tacctl2;

wire        tacctl2_wr = reg_wr[TACCTL2];
wire        ccifg2_set;
wire        ccifg2_clr;
wire        cov2_set;   
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)         tacctl2 <=  16'h0000;
  else if (tacctl2_wr) tacctl2 <=  ((per_din & 16'hf9f7) | {14'h0000, cov2_set, ccifg2_set}) & {15'h7fff, ~ccifg2_clr};
  else                 tacctl2 <=  (tacctl2              | {14'h0000, cov2_set, ccifg2_set}) & {15'h7fff, ~ccifg2_clr};

wire        cci2;
reg         scci2;
wire [15:0] tacctl2_full = tacctl2 | {5'h00, scci2, 6'h00, cci2, 3'h0};

   


reg  [15:0] taccr2;

wire        taccr2_wr = reg_wr[TACCR2];
wire        cci2_cap;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)        taccr2 <=  16'h0000;
  else if (taccr2_wr) taccr2 <=  per_din;
  else if (cci2_cap)  taccr2 <=  tar;

   



wire [3:0] taiv = (tacctl1[`TACCIFG] & tacctl1[`TACCIE]) ? 4'h2 : 
                  (tacctl2[`TACCIFG] & tacctl2[`TACCIE]) ? 4'h4 : 
                  (tactl[`TAIFG]     & tactl[`TAIE])     ? 4'hA : 
                                                           4'h0;

assign     ccifg1_clr = (reg_rd[TAIV] | reg_wr[TAIV]) & (taiv==4'h2);
assign     ccifg2_clr = (reg_rd[TAIV] | reg_wr[TAIV]) & (taiv==4'h4);
assign     taifg_clr  = (reg_rd[TAIV] | reg_wr[TAIV]) & (taiv==4'hA);







wire [15:0] tactl_rd   = {6'h00, tactl}  & {16{reg_rd[TACTL]}};
wire [15:0] tar_rd     = tar             & {16{reg_rd[TAR]}};
wire [15:0] tacctl0_rd = tacctl0_full    & {16{reg_rd[TACCTL0]}};
wire [15:0] taccr0_rd  = taccr0          & {16{reg_rd[TACCR0]}};
wire [15:0] tacctl1_rd = tacctl1_full    & {16{reg_rd[TACCTL1]}};
wire [15:0] taccr1_rd  = taccr1          & {16{reg_rd[TACCR1]}};
wire [15:0] tacctl2_rd = tacctl2_full    & {16{reg_rd[TACCTL2]}};
wire [15:0] taccr2_rd  = taccr2          & {16{reg_rd[TACCR2]}};
wire [15:0] taiv_rd    = {12'h000, taiv} & {16{reg_rd[TAIV]}};

wire [15:0] per_dout   =  tactl_rd   |
                          tar_rd     |
                          tacctl0_rd |
                          taccr0_rd  |
                          tacctl1_rd |
                          taccr1_rd  |
                          tacctl2_rd |
                          taccr2_rd  |
                          taiv_rd;

   






wire taclk_s;
wire inclk_s;

omsp_sync_cell sync_cell_taclk (
    .data_out  (taclk_s),
    .data_in   (taclk),
    .clk       (mclk),
    .rst       (puc_rst)
);

omsp_sync_cell sync_cell_inclk (
    .data_out  (inclk_s),
    .data_in   (inclk),
    .clk       (mclk),
    .rst       (puc_rst)
);





reg  taclk_dly;
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) taclk_dly <=  1'b0;
  else         taclk_dly <=  taclk_s;    

wire taclk_en = taclk_s & ~taclk_dly;

   
reg  inclk_dly;
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) inclk_dly <=  1'b0;
  else         inclk_dly <=  inclk_s;    

wire inclk_en = inclk_s & ~inclk_dly;

   



wire sel_clk = (tactl[`TASSELx]==2'b00) ? taclk_en :
               (tactl[`TASSELx]==2'b01) ?  aclk_en :
               (tactl[`TASSELx]==2'b10) ? smclk_en : inclk_en;

     


reg [2:0] clk_div;

assign    tar_clk = sel_clk & ((tactl[`TAIDx]==2'b00) ?  1'b1         :
                               (tactl[`TAIDx]==2'b01) ?  clk_div[0]   :
                               (tactl[`TAIDx]==2'b10) ? &clk_div[1:0] :
                                                        &clk_div[2:0]);
	  
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                               clk_div <=  3'h0;
  else if  (tar_clk | taclr)                 clk_div <=  3'h0;
  else if ((tactl[`TAMCx]!=2'b00) & sel_clk) clk_div <=  clk_div+3'h1;

  



assign  tar_clr   = ((tactl[`TAMCx]==2'b01) & (tar>=taccr0))         |
                    ((tactl[`TAMCx]==2'b11) & (taccr0==16'h0000));

assign  tar_inc   =  (tactl[`TAMCx]==2'b01) | (tactl[`TAMCx]==2'b10) | 
                    ((tactl[`TAMCx]==2'b11) & ~tar_dec);

reg tar_dir;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                        tar_dir <=  1'b0;
  else if (taclr)                     tar_dir <=  1'b0;
  else if (tactl[`TAMCx]==2'b11)
    begin
       if (tar_clk & (tar==16'h0001)) tar_dir <=  1'b0;
       else if       (tar>=taccr0)    tar_dir <=  1'b1;
    end
  else                                tar_dir <=  1'b0;
   
assign tar_dec = tar_dir | ((tactl[`TAMCx]==2'b11) & (tar>=taccr0));

   




wire equ0 = (tar_nxt==taccr0) & (tar!=taccr0);
wire equ1 = (tar_nxt==taccr1) & (tar!=taccr1);
wire equ2 = (tar_nxt==taccr2) & (tar!=taccr2);








assign cci0 = (tacctl0[`TACCISx]==2'b00) ? ta_cci0a :
              (tacctl0[`TACCISx]==2'b01) ? ta_cci0b :
              (tacctl0[`TACCISx]==2'b10) ?     1'b0 : 1'b1;

assign cci1 = (tacctl1[`TACCISx]==2'b00) ? ta_cci1a :
              (tacctl1[`TACCISx]==2'b01) ? ta_cci1b :
              (tacctl1[`TACCISx]==2'b10) ?     1'b0 : 1'b1;

assign cci2 = (tacctl2[`TACCISx]==2'b00) ? ta_cci2a :
              (tacctl2[`TACCISx]==2'b01) ? ta_cci2b :
              (tacctl2[`TACCISx]==2'b10) ?     1'b0 : 1'b1;


wire cci0_s;
wire cci1_s;
wire cci2_s;

omsp_sync_cell sync_cell_cci0 (
    .data_out (cci0_s),
    .data_in  (cci0),
    .clk      (mclk),
    .rst      (puc_rst)
);
omsp_sync_cell sync_cell_cci1 (
    .data_out (cci1_s),
    .data_in  (cci1),
    .clk      (mclk),
    .rst      (puc_rst)
);
omsp_sync_cell sync_cell_cci2 (
    .data_out (cci2_s),
    .data_in  (cci2),
    .clk      (mclk),
    .rst      (puc_rst)
);


reg cci0_dly;
reg cci1_dly;
reg cci2_dly;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)
    begin
       cci0_dly <=  1'b0;
       cci1_dly <=  1'b0;
       cci2_dly <=  1'b0;
    end
  else
    begin
       cci0_dly <=  cci0_s;
       cci1_dly <=  cci1_s;
       cci2_dly <=  cci2_s;
    end

   



always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             scci0 <=  1'b0;
  else if (tar_clk & equ0) scci0 <=  cci0_s;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             scci1 <=  1'b0;
  else if (tar_clk & equ1) scci1 <=  cci1_s;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             scci2 <=  1'b0;
  else if (tar_clk & equ2) scci2 <=  cci2_s;




wire cci0_evt = (tacctl0[`TACMx]==2'b00) ? 1'b0                  :
                (tacctl0[`TACMx]==2'b01) ? ( cci0_s & ~cci0_dly) :
                (tacctl0[`TACMx]==2'b10) ? (~cci0_s &  cci0_dly) :
                                           ( cci0_s ^  cci0_dly);

wire cci1_evt = (tacctl1[`TACMx]==2'b00) ? 1'b0                  :
                (tacctl1[`TACMx]==2'b01) ? ( cci1_s & ~cci1_dly) :
                (tacctl1[`TACMx]==2'b10) ? (~cci1_s &  cci1_dly) :
                                           ( cci1_s ^  cci1_dly);

wire cci2_evt = (tacctl2[`TACMx]==2'b00) ? 1'b0                  :
                (tacctl2[`TACMx]==2'b01) ? ( cci2_s & ~cci2_dly) :
                (tacctl2[`TACMx]==2'b10) ? (~cci2_s &  cci2_dly) :
                                           ( cci2_s ^  cci2_dly);




reg cci0_evt_s;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)       cci0_evt_s <=  1'b0;
  else if (tar_clk)  cci0_evt_s <=  1'b0;
  else if (cci0_evt) cci0_evt_s <=  1'b1;

reg cci1_evt_s;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)       cci1_evt_s <=  1'b0;
  else if (tar_clk)  cci1_evt_s <=  1'b0;
  else if (cci1_evt) cci1_evt_s <=  1'b1;

reg cci2_evt_s;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)       cci2_evt_s <=  1'b0;
  else if (tar_clk)  cci2_evt_s <=  1'b0;
  else if (cci2_evt) cci2_evt_s <=  1'b1;

reg cci0_sync;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) cci0_sync <=  1'b0;
  else         cci0_sync <=  (tar_clk & cci0_evt_s) | (tar_clk & cci0_evt & ~cci0_evt_s);

reg cci1_sync;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) cci1_sync <=  1'b0;
  else         cci1_sync <=  (tar_clk & cci1_evt_s) | (tar_clk & cci1_evt & ~cci1_evt_s);

reg cci2_sync;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) cci2_sync <=  1'b0;
  else         cci2_sync <=  (tar_clk & cci2_evt_s) | (tar_clk & cci2_evt & ~cci2_evt_s);

   



assign cci0_cap  = tacctl0[`TASCS] ? cci0_sync : cci0_evt;
assign cci1_cap  = tacctl1[`TASCS] ? cci1_sync : cci1_evt;
assign cci2_cap  = tacctl2[`TASCS] ? cci2_sync : cci2_evt;

   



reg  cap0_taken;
wire cap0_taken_clr = reg_rd[TACCR0] | (tacctl0_wr & tacctl0[`TACOV] & ~per_din[`TACOV]);
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             cap0_taken <=  1'b0;
  else if (cci0_cap)       cap0_taken <=  1'b1;
  else if (cap0_taken_clr) cap0_taken <=  1'b0;
   
reg  cap1_taken;
wire cap1_taken_clr = reg_rd[TACCR1] | (tacctl1_wr & tacctl1[`TACOV] & ~per_din[`TACOV]);
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             cap1_taken <=  1'b0;
  else if (cci1_cap)       cap1_taken <=  1'b1;
  else if (cap1_taken_clr) cap1_taken <=  1'b0;
      
reg  cap2_taken;
wire cap2_taken_clr = reg_rd[TACCR2] | (tacctl2_wr & tacctl2[`TACOV] & ~per_din[`TACOV]);
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             cap2_taken <=  1'b0;
  else if (cci2_cap)       cap2_taken <=  1'b1;
  else if (cap2_taken_clr) cap2_taken <=  1'b0;

   
assign cov0_set = cap0_taken & cci0_cap & ~reg_rd[TACCR0];
assign cov1_set = cap1_taken & cci1_cap & ~reg_rd[TACCR1];   
assign cov2_set = cap2_taken & cci2_cap & ~reg_rd[TACCR2];
  
      






reg  ta_out0;

wire ta_out0_mode0 = tacctl0[`TAOUT];
wire ta_out0_mode1 = equ0 ?  1'b1    : ta_out0;
wire ta_out0_mode2 = equ0 ? ~ta_out0 :
                     equ0 ?  1'b0    : ta_out0;
wire ta_out0_mode3 = equ0 ?  1'b1    :
                     equ0 ?  1'b0    : ta_out0;
wire ta_out0_mode4 = equ0 ? ~ta_out0 : ta_out0;
wire ta_out0_mode5 = equ0 ?  1'b0    : ta_out0;
wire ta_out0_mode6 = equ0 ? ~ta_out0 :
                     equ0 ?  1'b1    : ta_out0;
wire ta_out0_mode7 = equ0 ?  1'b0    :
                     equ0 ?  1'b1    : ta_out0;

wire ta_out0_nxt   = (tacctl0[`TAOUTMODx]==3'b000) ? ta_out0_mode0 :
                     (tacctl0[`TAOUTMODx]==3'b001) ? ta_out0_mode1 :
                     (tacctl0[`TAOUTMODx]==3'b010) ? ta_out0_mode2 :
                     (tacctl0[`TAOUTMODx]==3'b011) ? ta_out0_mode3 :
                     (tacctl0[`TAOUTMODx]==3'b100) ? ta_out0_mode4 :
                     (tacctl0[`TAOUTMODx]==3'b101) ? ta_out0_mode5 :
                     (tacctl0[`TAOUTMODx]==3'b110) ? ta_out0_mode6 :
                                                     ta_out0_mode7;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                                     ta_out0 <=  1'b0;
  else if ((tacctl0[`TAOUTMODx]==3'b001) & taclr)  ta_out0 <=  1'b0;
  else if (tar_clk)                                ta_out0 <=  ta_out0_nxt;

assign  ta_out0_en = ~tacctl0[`TACAP];

   


reg  ta_out1;

wire ta_out1_mode0 = tacctl1[`TAOUT];
wire ta_out1_mode1 = equ1 ?  1'b1    : ta_out1;
wire ta_out1_mode2 = equ1 ? ~ta_out1 :
                     equ0 ?  1'b0    : ta_out1;
wire ta_out1_mode3 = equ1 ?  1'b1    :
                     equ0 ?  1'b0    : ta_out1;
wire ta_out1_mode4 = equ1 ? ~ta_out1 : ta_out1;
wire ta_out1_mode5 = equ1 ?  1'b0    : ta_out1;
wire ta_out1_mode6 = equ1 ? ~ta_out1 :
                     equ0 ?  1'b1    : ta_out1;
wire ta_out1_mode7 = equ1 ?  1'b0    :
                     equ0 ?  1'b1    : ta_out1;

wire ta_out1_nxt   = (tacctl1[`TAOUTMODx]==3'b000) ? ta_out1_mode0 :
                     (tacctl1[`TAOUTMODx]==3'b001) ? ta_out1_mode1 :
                     (tacctl1[`TAOUTMODx]==3'b010) ? ta_out1_mode2 :
                     (tacctl1[`TAOUTMODx]==3'b011) ? ta_out1_mode3 :
                     (tacctl1[`TAOUTMODx]==3'b100) ? ta_out1_mode4 :
                     (tacctl1[`TAOUTMODx]==3'b101) ? ta_out1_mode5 :
                     (tacctl1[`TAOUTMODx]==3'b110) ? ta_out1_mode6 :
                                                     ta_out1_mode7;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                                     ta_out1 <=  1'b0;
  else if ((tacctl1[`TAOUTMODx]==3'b001) & taclr)  ta_out1 <=  1'b0;
  else if (tar_clk)                                ta_out1 <=  ta_out1_nxt;

assign  ta_out1_en = ~tacctl1[`TACAP];

   


reg  ta_out2;

wire ta_out2_mode0 = tacctl2[`TAOUT];
wire ta_out2_mode1 = equ2 ?  1'b1    : ta_out2;
wire ta_out2_mode2 = equ2 ? ~ta_out2 :
                     equ0 ?  1'b0    : ta_out2;
wire ta_out2_mode3 = equ2 ?  1'b1    :
                     equ0 ?  1'b0    : ta_out2;
wire ta_out2_mode4 = equ2 ? ~ta_out2 : ta_out2;
wire ta_out2_mode5 = equ2 ?  1'b0    : ta_out2;
wire ta_out2_mode6 = equ2 ? ~ta_out2 :
                     equ0 ?  1'b1    : ta_out2;
wire ta_out2_mode7 = equ2 ?  1'b0    :
                     equ0 ?  1'b1    : ta_out2;

wire ta_out2_nxt   = (tacctl2[`TAOUTMODx]==3'b000) ? ta_out2_mode0 :
                     (tacctl2[`TAOUTMODx]==3'b001) ? ta_out2_mode1 :
                     (tacctl2[`TAOUTMODx]==3'b010) ? ta_out2_mode2 :
                     (tacctl2[`TAOUTMODx]==3'b011) ? ta_out2_mode3 :
                     (tacctl2[`TAOUTMODx]==3'b100) ? ta_out2_mode4 :
                     (tacctl2[`TAOUTMODx]==3'b101) ? ta_out2_mode5 :
                     (tacctl2[`TAOUTMODx]==3'b110) ? ta_out2_mode6 :
                                                     ta_out2_mode7;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                                     ta_out2 <=  1'b0;
  else if ((tacctl2[`TAOUTMODx]==3'b001) & taclr)  ta_out2 <=  1'b0;
  else if (tar_clk)                                ta_out2 <=  ta_out2_nxt;

assign  ta_out2_en = ~tacctl2[`TACAP];

   





assign   taifg_set   = tar_clk & (((tactl[`TAMCx]==2'b01) & (tar==taccr0))                  |
                                  ((tactl[`TAMCx]==2'b10) & (tar==16'hffff))                |
                                  ((tactl[`TAMCx]==2'b11) & (tar_nxt==16'h0000) & tar_dec));

assign   ccifg0_set  = tacctl0[`TACAP] ? cci0_cap : (tar_clk &  ((tactl[`TAMCx]!=2'b00) & equ0));
assign   ccifg1_set  = tacctl1[`TACAP] ? cci1_cap : (tar_clk &  ((tactl[`TAMCx]!=2'b00) & equ1));
assign   ccifg2_set  = tacctl2[`TACAP] ? cci2_cap : (tar_clk &  ((tactl[`TAMCx]!=2'b00) & equ2));

  
wire     irq_ta0    = (tacctl0[`TACCIFG] & tacctl0[`TACCIE]);

wire     irq_ta1    = (tactl[`TAIFG]     & tactl[`TAIE])     |
                      (tacctl1[`TACCIFG] & tacctl1[`TACCIE]) |
                      (tacctl2[`TACCIFG] & tacctl2[`TACCIE]);
   

endmodule
module  omsp_timerA (


    irq_ta0,
    irq_ta1,
    per_dout,
    ta_out0,
    ta_out0_en,
    ta_out1,
    ta_out1_en,
    ta_out2,
    ta_out2_en,


    aclk_en,
    dbg_freeze,
    inclk,
    irq_ta0_acc,
    mclk,
    per_addr,
    per_din,
    per_en,
    per_we,
    puc_rst,
    smclk_en,
    ta_cci0a,
    ta_cci0b,
    ta_cci1a,
    ta_cci1b,
    ta_cci2a,
    ta_cci2b,
    taclk
);




output              irq_ta0;
output              irq_ta1;
output       [15:0] per_dout;
output              ta_out0;
output              ta_out0_en;
output              ta_out1;
output              ta_out1_en;
output              ta_out2;
output              ta_out2_en;



input               aclk_en;
input               dbg_freeze;
input               inclk;
input               irq_ta0_acc;
input               mclk;
input        [13:0] per_addr;
input        [15:0] per_din;
input               per_en;
input         [1:0] per_we;
input               puc_rst;
input               smclk_en;
input               ta_cci0a;
input               ta_cci0b;
input               ta_cci1a;
input               ta_cci1b;
input               ta_cci2a;
input               ta_cci2b;
input               taclk;







parameter       [14:0] BASE_ADDR  = 15'h0100;


parameter              DEC_WD     =  7;


parameter [DEC_WD-1:0] TACTL      = 'h60,
                       TAR        = 'h70,
                       TACCTL0    = 'h62,
                       TACCR0     = 'h72,
                       TACCTL1    = 'h64,
                       TACCR1     = 'h74,
                       TACCTL2    = 'h66,
                       TACCR2     = 'h76,
                       TAIV       = 'h2E;


parameter              DEC_SZ     =  (1 << DEC_WD);
parameter [DEC_SZ-1:0] BASE_REG   =  {{DEC_SZ-1{1'b0}}, 1'b1};


parameter [DEC_SZ-1:0] TACTL_D    = (BASE_REG << TACTL),
                       TAR_D      = (BASE_REG << TAR),
                       TACCTL0_D  = (BASE_REG << TACCTL0),
                       TACCR0_D   = (BASE_REG << TACCR0),
                       TACCTL1_D  = (BASE_REG << TACCTL1),
                       TACCR1_D   = (BASE_REG << TACCR1),
                       TACCTL2_D  = (BASE_REG << TACCTL2),
                       TACCR2_D   = (BASE_REG << TACCR2),
                       TAIV_D     = (BASE_REG << TAIV);







wire              reg_sel   =  per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);


wire [DEC_WD-1:0] reg_addr  =  {per_addr[DEC_WD-2:0], 1'b0};


wire [DEC_SZ-1:0] reg_dec   =  (TACTL_D    &  {DEC_SZ{(reg_addr == TACTL   )}})  |
                               (TAR_D      &  {DEC_SZ{(reg_addr == TAR     )}})  |
                               (TACCTL0_D  &  {DEC_SZ{(reg_addr == TACCTL0 )}})  |
                               (TACCR0_D   &  {DEC_SZ{(reg_addr == TACCR0  )}})  |
                               (TACCTL1_D  &  {DEC_SZ{(reg_addr == TACCTL1 )}})  |
                               (TACCR1_D   &  {DEC_SZ{(reg_addr == TACCR1  )}})  |
                               (TACCTL2_D  &  {DEC_SZ{(reg_addr == TACCTL2 )}})  |
                               (TACCR2_D   &  {DEC_SZ{(reg_addr == TACCR2  )}})  |
                               (TAIV_D     &  {DEC_SZ{(reg_addr == TAIV    )}});


wire              reg_write =  |per_we & reg_sel;
wire              reg_read  = ~|per_we & reg_sel;


wire [DEC_SZ-1:0] reg_wr    = reg_dec & {512{reg_write}};
wire [DEC_SZ-1:0] reg_rd    = reg_dec & {512{reg_read}};








reg   [9:0] tactl;

wire        tactl_wr = reg_wr[TACTL];
wire        taclr    = tactl_wr & per_din[`TACLR];
wire        taifg_set;
wire        taifg_clr;
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)       tactl <=  10'h000;
  else if (tactl_wr) tactl <=  ((per_din[9:0] & 10'h3f3) | {9'h000, taifg_set}) & {9'h1ff, ~taifg_clr};
  else               tactl <=  (tactl                    | {9'h000, taifg_set}) & {9'h1ff, ~taifg_clr};




reg  [15:0] tar;

wire        tar_wr = reg_wr[TAR];

wire        tar_clk;
wire        tar_clr;
wire        tar_inc;
wire        tar_dec;
wire [15:0] tar_add  = tar_inc ? 16'h0001 :
                       tar_dec ? 16'hffff : 16'h0000;
wire [15:0] tar_nxt  = tar_clr ? 16'h0000 : (tar+tar_add);
  
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                     tar <=  16'h0000;
  else if  (tar_wr)                tar <=  per_din;
  else if  (taclr)                 tar <=  16'h0000;
  else if  (tar_clk & ~dbg_freeze) tar <=  tar_nxt;




reg  [15:0] tacctl0;

wire        tacctl0_wr = reg_wr[TACCTL0];
wire        ccifg0_set;
wire        cov0_set;   

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)         tacctl0  <=  16'h0000;
  else if (tacctl0_wr) tacctl0  <=  ((per_din & 16'hf9f7) | {14'h0000, cov0_set, ccifg0_set}) & {15'h7fff, ~irq_ta0_acc};
  else                 tacctl0  <=  (tacctl0              | {14'h0000, cov0_set, ccifg0_set}) & {15'h7fff, ~irq_ta0_acc};

wire        cci0;
reg         scci0;
wire [15:0] tacctl0_full = tacctl0 | {5'h00, scci0, 6'h00, cci0, 3'h0};

   


reg  [15:0] taccr0;

wire        taccr0_wr = reg_wr[TACCR0];
wire        cci0_cap;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)        taccr0 <=  16'h0000;
  else if (taccr0_wr) taccr0 <=  per_din;
  else if (cci0_cap)  taccr0 <=  tar;

   


reg  [15:0] tacctl1;

wire        tacctl1_wr = reg_wr[TACCTL1];
wire        ccifg1_set;
wire        ccifg1_clr;
wire        cov1_set;   
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)         tacctl1 <=  16'h0000;
  else if (tacctl1_wr) tacctl1 <=  ((per_din & 16'hf9f7) | {14'h0000, cov1_set, ccifg1_set}) & {15'h7fff, ~ccifg1_clr};
  else                 tacctl1 <=  (tacctl1              | {14'h0000, cov1_set, ccifg1_set}) & {15'h7fff, ~ccifg1_clr};

wire        cci1;
reg         scci1;
wire [15:0] tacctl1_full = tacctl1 | {5'h00, scci1, 6'h00, cci1, 3'h0};

   


reg  [15:0] taccr1;

wire        taccr1_wr = reg_wr[TACCR1];
wire        cci1_cap;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)        taccr1 <=  16'h0000;
  else if (taccr1_wr) taccr1 <=  per_din;
  else if (cci1_cap)  taccr1 <=  tar;




reg  [15:0] tacctl2;

wire        tacctl2_wr = reg_wr[TACCTL2];
wire        ccifg2_set;
wire        ccifg2_clr;
wire        cov2_set;   
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)         tacctl2 <=  16'h0000;
  else if (tacctl2_wr) tacctl2 <=  ((per_din & 16'hf9f7) | {14'h0000, cov2_set, ccifg2_set}) & {15'h7fff, ~ccifg2_clr};
  else                 tacctl2 <=  (tacctl2              | {14'h0000, cov2_set, ccifg2_set}) & {15'h7fff, ~ccifg2_clr};

wire        cci2;
reg         scci2;
wire [15:0] tacctl2_full = tacctl2 | {5'h00, scci2, 6'h00, cci2, 3'h0};

   


reg  [15:0] taccr2;

wire        taccr2_wr = reg_wr[TACCR2];
wire        cci2_cap;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)        taccr2 <=  16'h0000;
  else if (taccr2_wr) taccr2 <=  per_din;
  else if (cci2_cap)  taccr2 <=  tar;

   



wire [3:0] taiv = (tacctl1[`TACCIFG] & tacctl1[`TACCIE]) ? 4'h2 : 
                  (tacctl2[`TACCIFG] & tacctl2[`TACCIE]) ? 4'h4 : 
                  (tactl[`TAIFG]     & tactl[`TAIE])     ? 4'hA : 
                                                           4'h0;

assign     ccifg1_clr = (reg_rd[TAIV] | reg_wr[TAIV]) & (taiv==4'h2);
assign     ccifg2_clr = (reg_rd[TAIV] | reg_wr[TAIV]) & (taiv==4'h4);
assign     taifg_clr  = (reg_rd[TAIV] | reg_wr[TAIV]) & (taiv==4'hA);







wire [15:0] tactl_rd   = {6'h00, tactl}  & {16{reg_rd[TACTL]}};
wire [15:0] tar_rd     = tar             & {16{reg_rd[TAR]}};
wire [15:0] tacctl0_rd = tacctl0_full    & {16{reg_rd[TACCTL0]}};
wire [15:0] taccr0_rd  = taccr0          & {16{reg_rd[TACCR0]}};
wire [15:0] tacctl1_rd = tacctl1_full    & {16{reg_rd[TACCTL1]}};
wire [15:0] taccr1_rd  = taccr1          & {16{reg_rd[TACCR1]}};
wire [15:0] tacctl2_rd = tacctl2_full    & {16{reg_rd[TACCTL2]}};
wire [15:0] taccr2_rd  = taccr2          & {16{reg_rd[TACCR2]}};
wire [15:0] taiv_rd    = {12'h000, taiv} & {16{reg_rd[TAIV]}};

wire [15:0] per_dout   =  tactl_rd   |
                          tar_rd     |
                          tacctl0_rd |
                          taccr0_rd  |
                          tacctl1_rd |
                          taccr1_rd  |
                          tacctl2_rd |
                          taccr2_rd  |
                          taiv_rd;

   






wire taclk_s;
wire inclk_s;

omsp_sync_cell sync_cell_taclk (
    .data_out  (taclk_s),
    .data_in   (taclk),
    .clk       (mclk),
    .rst       (puc_rst)
);

omsp_sync_cell sync_cell_inclk (
    .data_out  (inclk_s),
    .data_in   (inclk),
    .clk       (mclk),
    .rst       (puc_rst)
);





reg  taclk_dly;
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) taclk_dly <=  1'b0;
  else         taclk_dly <=  taclk_s;    

wire taclk_en = taclk_s & ~taclk_dly;

   
reg  inclk_dly;
   
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) inclk_dly <=  1'b0;
  else         inclk_dly <=  inclk_s;    

wire inclk_en = inclk_s & ~inclk_dly;

   



wire sel_clk = (tactl[`TASSELx]==2'b00) ? taclk_en :
               (tactl[`TASSELx]==2'b01) ?  aclk_en :
               (tactl[`TASSELx]==2'b10) ? smclk_en : inclk_en;

     


reg [2:0] clk_div;

assign    tar_clk = sel_clk & ((tactl[`TAIDx]==2'b00) ?  1'b1         :
                               (tactl[`TAIDx]==2'b01) ?  clk_div[0]   :
                               (tactl[`TAIDx]==2'b10) ? &clk_div[1:0] :
                                                        &clk_div[2:0]);
	  
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                               clk_div <=  3'h0;
  else if  (tar_clk | taclr)                 clk_div <=  3'h0;
  else if ((tactl[`TAMCx]!=2'b00) & sel_clk) clk_div <=  clk_div+3'h1;

  



assign  tar_clr   = ((tactl[`TAMCx]==2'b01) & (tar>=taccr0))         |
                    ((tactl[`TAMCx]==2'b11) & (taccr0==16'h0000));

assign  tar_inc   =  (tactl[`TAMCx]==2'b01) | (tactl[`TAMCx]==2'b10) | 
                    ((tactl[`TAMCx]==2'b11) & ~tar_dec);

reg tar_dir;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                        tar_dir <=  1'b0;
  else if (taclr)                     tar_dir <=  1'b0;
  else if (tactl[`TAMCx]==2'b11)
    begin
       if (tar_clk & (tar==16'h0001)) tar_dir <=  1'b0;
       else if       (tar>=taccr0)    tar_dir <=  1'b1;
    end
  else                                tar_dir <=  1'b0;
   
assign tar_dec = tar_dir | ((tactl[`TAMCx]==2'b11) & (tar>=taccr0));

   




wire equ0 = (tar_nxt==taccr0) & (tar!=taccr0);
wire equ1 = (tar_nxt==taccr1) & (tar!=taccr1);
wire equ2 = (tar_nxt==taccr2) & (tar!=taccr2);








assign cci0 = (tacctl0[`TACCISx]==2'b00) ? ta_cci0a :
              (tacctl0[`TACCISx]==2'b01) ? ta_cci0b :
              (tacctl0[`TACCISx]==2'b10) ?     1'b0 : 1'b1;

assign cci1 = (tacctl1[`TACCISx]==2'b00) ? ta_cci1a :
              (tacctl1[`TACCISx]==2'b01) ? ta_cci1b :
              (tacctl1[`TACCISx]==2'b10) ?     1'b0 : 1'b1;

assign cci2 = (tacctl2[`TACCISx]==2'b00) ? ta_cci2a :
              (tacctl2[`TACCISx]==2'b01) ? ta_cci2b :
              (tacctl2[`TACCISx]==2'b10) ?     1'b0 : 1'b1;


wire cci0_s;
wire cci1_s;
wire cci2_s;

omsp_sync_cell sync_cell_cci0 (
    .data_out (cci0_s),
    .data_in  (cci0),
    .clk      (mclk),
    .rst      (puc_rst)
);
omsp_sync_cell sync_cell_cci1 (
    .data_out (cci1_s),
    .data_in  (cci1),
    .clk      (mclk),
    .rst      (puc_rst)
);
omsp_sync_cell sync_cell_cci2 (
    .data_out (cci2_s),
    .data_in  (cci2),
    .clk      (mclk),
    .rst      (puc_rst)
);


reg cci0_dly;
reg cci1_dly;
reg cci2_dly;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)
    begin
       cci0_dly <=  1'b0;
       cci1_dly <=  1'b0;
       cci2_dly <=  1'b0;
    end
  else
    begin
       cci0_dly <=  cci0_s;
       cci1_dly <=  cci1_s;
       cci2_dly <=  cci2_s;
    end

   



always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             scci0 <=  1'b0;
  else if (tar_clk & equ0) scci0 <=  cci0_s;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             scci1 <=  1'b0;
  else if (tar_clk & equ1) scci1 <=  cci1_s;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             scci2 <=  1'b0;
  else if (tar_clk & equ2) scci2 <=  cci2_s;




wire cci0_evt = (tacctl0[`TACMx]==2'b00) ? 1'b0                  :
                (tacctl0[`TACMx]==2'b01) ? ( cci0_s & ~cci0_dly) :
                (tacctl0[`TACMx]==2'b10) ? (~cci0_s &  cci0_dly) :
                                           ( cci0_s ^  cci0_dly);

wire cci1_evt = (tacctl1[`TACMx]==2'b00) ? 1'b0                  :
                (tacctl1[`TACMx]==2'b01) ? ( cci1_s & ~cci1_dly) :
                (tacctl1[`TACMx]==2'b10) ? (~cci1_s &  cci1_dly) :
                                           ( cci1_s ^  cci1_dly);

wire cci2_evt = (tacctl2[`TACMx]==2'b00) ? 1'b0                  :
                (tacctl2[`TACMx]==2'b01) ? ( cci2_s & ~cci2_dly) :
                (tacctl2[`TACMx]==2'b10) ? (~cci2_s &  cci2_dly) :
                                           ( cci2_s ^  cci2_dly);




reg cci0_evt_s;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)       cci0_evt_s <=  1'b0;
  else if (tar_clk)  cci0_evt_s <=  1'b0;
  else if (cci0_evt) cci0_evt_s <=  1'b1;

reg cci1_evt_s;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)       cci1_evt_s <=  1'b0;
  else if (tar_clk)  cci1_evt_s <=  1'b0;
  else if (cci1_evt) cci1_evt_s <=  1'b1;

reg cci2_evt_s;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)       cci2_evt_s <=  1'b0;
  else if (tar_clk)  cci2_evt_s <=  1'b0;
  else if (cci2_evt) cci2_evt_s <=  1'b1;

reg cci0_sync;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) cci0_sync <=  1'b0;
  else         cci0_sync <=  (tar_clk & cci0_evt_s) | (tar_clk & cci0_evt & ~cci0_evt_s);

reg cci1_sync;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) cci1_sync <=  1'b0;
  else         cci1_sync <=  (tar_clk & cci1_evt_s) | (tar_clk & cci1_evt & ~cci1_evt_s);

reg cci2_sync;
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst) cci2_sync <=  1'b0;
  else         cci2_sync <=  (tar_clk & cci2_evt_s) | (tar_clk & cci2_evt & ~cci2_evt_s);

   



assign cci0_cap  = tacctl0[`TASCS] ? cci0_sync : cci0_evt;
assign cci1_cap  = tacctl1[`TASCS] ? cci1_sync : cci1_evt;
assign cci2_cap  = tacctl2[`TASCS] ? cci2_sync : cci2_evt;

   



reg  cap0_taken;
wire cap0_taken_clr = reg_rd[TACCR0] | (tacctl0_wr & tacctl0[`TACOV] & ~per_din[`TACOV]);
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             cap0_taken <=  1'b0;
  else if (cci0_cap)       cap0_taken <=  1'b1;
  else if (cap0_taken_clr) cap0_taken <=  1'b0;
   
reg  cap1_taken;
wire cap1_taken_clr = reg_rd[TACCR1] | (tacctl1_wr & tacctl1[`TACOV] & ~per_din[`TACOV]);
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             cap1_taken <=  1'b0;
  else if (cci1_cap)       cap1_taken <=  1'b1;
  else if (cap1_taken_clr) cap1_taken <=  1'b0;
      
reg  cap2_taken;
wire cap2_taken_clr = reg_rd[TACCR2] | (tacctl2_wr & tacctl2[`TACOV] & ~per_din[`TACOV]);
always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)             cap2_taken <=  1'b0;
  else if (cci2_cap)       cap2_taken <=  1'b1;
  else if (cap2_taken_clr) cap2_taken <=  1'b0;

   
assign cov0_set = cap0_taken & cci0_cap & ~reg_rd[TACCR0];
assign cov1_set = cap1_taken & cci1_cap & ~reg_rd[TACCR1];   
assign cov2_set = cap2_taken & cci2_cap & ~reg_rd[TACCR2];
  
      






reg  ta_out0;

wire ta_out0_mode0 = tacctl0[`TAOUT];
wire ta_out0_mode1 = equ0 ?  1'b1    : ta_out0;
wire ta_out0_mode2 = equ0 ? ~ta_out0 :
                     equ0 ?  1'b0    : ta_out0;
wire ta_out0_mode3 = equ0 ?  1'b1    :
                     equ0 ?  1'b0    : ta_out0;
wire ta_out0_mode4 = equ0 ? ~ta_out0 : ta_out0;
wire ta_out0_mode5 = equ0 ?  1'b0    : ta_out0;
wire ta_out0_mode6 = equ0 ? ~ta_out0 :
                     equ0 ?  1'b1    : ta_out0;
wire ta_out0_mode7 = equ0 ?  1'b0    :
                     equ0 ?  1'b1    : ta_out0;

wire ta_out0_nxt   = (tacctl0[`TAOUTMODx]==3'b000) ? ta_out0_mode0 :
                     (tacctl0[`TAOUTMODx]==3'b001) ? ta_out0_mode1 :
                     (tacctl0[`TAOUTMODx]==3'b010) ? ta_out0_mode2 :
                     (tacctl0[`TAOUTMODx]==3'b011) ? ta_out0_mode3 :
                     (tacctl0[`TAOUTMODx]==3'b100) ? ta_out0_mode4 :
                     (tacctl0[`TAOUTMODx]==3'b101) ? ta_out0_mode5 :
                     (tacctl0[`TAOUTMODx]==3'b110) ? ta_out0_mode6 :
                                                     ta_out0_mode7;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                                     ta_out0 <=  1'b0;
  else if ((tacctl0[`TAOUTMODx]==3'b001) & taclr)  ta_out0 <=  1'b0;
  else if (tar_clk)                                ta_out0 <=  ta_out0_nxt;

assign  ta_out0_en = ~tacctl0[`TACAP];

   


reg  ta_out1;

wire ta_out1_mode0 = tacctl1[`TAOUT];
wire ta_out1_mode1 = equ1 ?  1'b1    : ta_out1;
wire ta_out1_mode2 = equ1 ? ~ta_out1 :
                     equ0 ?  1'b0    : ta_out1;
wire ta_out1_mode3 = equ1 ?  1'b1    :
                     equ0 ?  1'b0    : ta_out1;
wire ta_out1_mode4 = equ1 ? ~ta_out1 : ta_out1;
wire ta_out1_mode5 = equ1 ?  1'b0    : ta_out1;
wire ta_out1_mode6 = equ1 ? ~ta_out1 :
                     equ0 ?  1'b1    : ta_out1;
wire ta_out1_mode7 = equ1 ?  1'b0    :
                     equ0 ?  1'b1    : ta_out1;

wire ta_out1_nxt   = (tacctl1[`TAOUTMODx]==3'b000) ? ta_out1_mode0 :
                     (tacctl1[`TAOUTMODx]==3'b001) ? ta_out1_mode1 :
                     (tacctl1[`TAOUTMODx]==3'b010) ? ta_out1_mode2 :
                     (tacctl1[`TAOUTMODx]==3'b011) ? ta_out1_mode3 :
                     (tacctl1[`TAOUTMODx]==3'b100) ? ta_out1_mode4 :
                     (tacctl1[`TAOUTMODx]==3'b101) ? ta_out1_mode5 :
                     (tacctl1[`TAOUTMODx]==3'b110) ? ta_out1_mode6 :
                                                     ta_out1_mode7;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                                     ta_out1 <=  1'b0;
  else if ((tacctl1[`TAOUTMODx]==3'b001) & taclr)  ta_out1 <=  1'b0;
  else if (tar_clk)                                ta_out1 <=  ta_out1_nxt;

assign  ta_out1_en = ~tacctl1[`TACAP];

   


reg  ta_out2;

wire ta_out2_mode0 = tacctl2[`TAOUT];
wire ta_out2_mode1 = equ2 ?  1'b1    : ta_out2;
wire ta_out2_mode2 = equ2 ? ~ta_out2 :
                     equ0 ?  1'b0    : ta_out2;
wire ta_out2_mode3 = equ2 ?  1'b1    :
                     equ0 ?  1'b0    : ta_out2;
wire ta_out2_mode4 = equ2 ? ~ta_out2 : ta_out2;
wire ta_out2_mode5 = equ2 ?  1'b0    : ta_out2;
wire ta_out2_mode6 = equ2 ? ~ta_out2 :
                     equ0 ?  1'b1    : ta_out2;
wire ta_out2_mode7 = equ2 ?  1'b0    :
                     equ0 ?  1'b1    : ta_out2;

wire ta_out2_nxt   = (tacctl2[`TAOUTMODx]==3'b000) ? ta_out2_mode0 :
                     (tacctl2[`TAOUTMODx]==3'b001) ? ta_out2_mode1 :
                     (tacctl2[`TAOUTMODx]==3'b010) ? ta_out2_mode2 :
                     (tacctl2[`TAOUTMODx]==3'b011) ? ta_out2_mode3 :
                     (tacctl2[`TAOUTMODx]==3'b100) ? ta_out2_mode4 :
                     (tacctl2[`TAOUTMODx]==3'b101) ? ta_out2_mode5 :
                     (tacctl2[`TAOUTMODx]==3'b110) ? ta_out2_mode6 :
                                                     ta_out2_mode7;

always @ (posedge mclk or posedge puc_rst)
  if (puc_rst)                                     ta_out2 <=  1'b0;
  else if ((tacctl2[`TAOUTMODx]==3'b001) & taclr)  ta_out2 <=  1'b0;
  else if (tar_clk)                                ta_out2 <=  ta_out2_nxt;

assign  ta_out2_en = ~tacctl2[`TACAP];

   





assign   taifg_set   = tar_clk & (((tactl[`TAMCx]==2'b01) & (tar==taccr0))                  |
                                  ((tactl[`TAMCx]==2'b10) & (tar==16'hffff))                |
                                  ((tactl[`TAMCx]==2'b11) & (tar_nxt==16'h0000) & tar_dec));

assign   ccifg0_set  = tacctl0[`TACAP] ? cci0_cap : (tar_clk &  ((tactl[`TAMCx]!=2'b00) & equ0));
assign   ccifg1_set  = tacctl1[`TACAP] ? cci1_cap : (tar_clk &  ((tactl[`TAMCx]!=2'b00) & equ1));
assign   ccifg2_set  = tacctl2[`TACAP] ? cci2_cap : (tar_clk &  ((tactl[`TAMCx]!=2'b00) & equ2));

  
wire     irq_ta0    = (tacctl0[`TACCIFG] & tacctl0[`TACCIE]);

wire     irq_ta1    = (tactl[`TAIFG]     & tactl[`TAIE])     |
                      (tacctl1[`TACCIFG] & tacctl1[`TACCIE]) |
                      (tacctl2[`TACCIFG] & tacctl2[`TACCIE]);
   

endmodule