// [label("输入时钟频率(khz)")|range(1, 1000000)|nobar|tooltip("输入时钟频率")]
// > clk_khz = 1000  ;# input clk frequency in KHz
// [label("输出PWM频率(hz)")|range(1, 50000000)|nobar|tooltip("PWM输出频率")]
// > pwm_hz = 1000  ;# pwm frequency in Hz
// [label("最大计数值")|tooltip("满足指定频率的计数器所能达到的最大的计数值")|readonly]
// > max_cnt = round(1000 * clk_khz / pwm_hz)
// [label("误差率(%)")|readonly|color("#FF0000")|tooltip("小于0时表示实际时钟频率偏低; 大于0反之")]
// > error_rate = f"{((clk_khz * 1000 - pwm_hz * max_cnt) / clk_khz / 1000 * 100):.2f}%"
// > tim_width = api_cwidth(max_cnt)
// [label("启用使能端口")|tooltip("启用后，当计数器溢出时，输出一个脉冲")]
// > port_en = False
// [label("启用溢出脉冲端口")|tooltip("启用后，当计数器溢出时，输出一个脉冲")]
// > port_rco = False
// [readonly|tooltip("rco信号持续的时长(2^rco_n)个clk周期")|label("rco持续位宽")|when(port_rco)]
//> rco_n=tim_width//4
// [label("输出通道数")|range(1, 4)|tooltip("PWM输出通道数")]
// > ch_num = 4  ;# pwm output channel number
// ----------------------------------- CH0 -----------------------------------
// [group("ch0")]
// [label("启用动态参数化")|when(ch_num > 0)|tooltip("启用后，请根据'最大计数值'自行确定cycle的值")]
// > param0 = False
// [label("翻转极性")|tooltip("False: 先是低电平，后是高电平; True: 先是高电平，后是低电平")|when(ch_num > 0 and not param0)]
// > polar0 = False  
// [label("占空比(%)")|range(0, 100.0, 0.01)|when(ch_num > 0 and not param0)]
// > cycle0 = 50.0
// [label("对应比较值")|when(ch_num > 0 and not param0)|readonly]
// > cmp0 = floor(max_cnt * cycle0 / 100)
// ----------------------------------- CH1 -----------------------------------
// [group("ch1")]
// [label("启用动态参数化")|when(ch_num > 1)|tooltip("启用后，请根据'最大计数值'自行确定cycle的值")]
// > param1 = False
// [label("翻转极性")|tooltip("False: 先是低电平，后是高电平; True: 先是高电平，后是低电平")|when(ch_num > 1 and not param1)]
// > polar1 = False
// [label("占空比(%)")|range(0, 100.0, 0.01)|when(ch_num > 1 and not param1)]
// > cycle1 = 50.0
// [label("对应比较值")|when(ch_num > 1 and not param1)|readonly]
// > cmp1 = int(max_cnt * cycle1 / 100)
// ----------------------------------- CH2 -----------------------------------
// [group("ch2")]
// [label("启用动态参数化")|when(ch_num > 2)|tooltip("启用后，请根据'最大计数值'自行确定cycle的值")]
// > param2 = False
// [label("翻转极性")|tooltip("False: 先是低电平，后是高电平; True: 先是高电平，后是低电平")|when(ch_num > 2 and not param2)]
// > polar2 = False
// [label("占空比(%)")|range(0, 100.0, 0.01)|when(ch_num > 2 and not param2)]
// > cycle2 = 50.0
// [label("对应比较值")|when(ch_num > 2 and not param2)|readonly]
// > cmp2 = int(max_cnt * cycle2 / 100)
// ----------------------------------- CH3 -----------------------------------
// [group("ch3")]
// [label("启用动态参数化")|when(ch_num > 3)|tooltip("启用后，请根据'最大计数值'自行确定cycle的值")]
// > param3 = False
// [label("翻转极性")|tooltip("False: 先是低电平，后是高电平; True: 先是高电平，后是低电平")|when(ch_num > 3 and not param3)]
// > polar3 = False
// [label("占空比(%)")|range(0, 100.0, 0.01)|when(ch_num > 3 and not param3)]
// > cycle3 = 50.0
// [label("对应比较值")|when(ch_num > 3 and not param3)|readonly]
// > cmp3 = int(max_cnt * cycle3 / 100)


/*> ;# python
pwm_suffix = pwm_hz
if not pwm_hz % 1000:
    pwm_suffix = f"{pwm_hz // 1000}khz"
if not pwm_hz % 1000000:
    pwm_suffix = f"{pwm_hz // 1000000}mhz"

*/

/*>
# ----------------------------------- CH0 -----------------------------------
port0 = '''
    input wire polor0,
    input wire [$tim_width-1$:0] cycle0,
'''
port0 = port0 if param0 and ch_num > 0 else ""
polor_sign0 = "polor0" if param0 else "PWM_PLOAR0"
local0 = '''
    localparam PWM_PLOAR0 = 1'b$int(polar0)$;
    localparam PWM_CYCLE0 = $tim_width$'d$cmp0$;
'''
local0 = local0 if not param0 and ch_num > 0 else ""
cycle_sign0 = "cycle0" if param0 else "PWM_CYCLE0"
assign0 = "oc[0] = (tim < $cycle_sign0$) ? $polor_sign0$ : ~$polor_sign0$;" if ch_num > 0 else ""
# ----------------------------------- CH1 -----------------------------------
port1 = '''
    input wire polor1,
    input wire [$tim_width-1$:0] cycle1,
'''
port1 = port1 if param1 and ch_num > 1 else ""
polor_sign1 = "polor1" if param1 else "PWM_PLOAR1"
local1 = '''
    localparam PWM_PLOAR1 = 1'b$int(polar1)$;
    localparam PWM_CYCLE1 = $tim_width$'d$cmp1$;
'''
local1 = local1 if not param1 and ch_num > 1 else ""
cycle_sign1 = "cycle1" if param1 else "PWM_CYCLE1"
assign1 = "oc[1] = (tim < $cycle_sign1$) ? $polor_sign1$ : ~$polor_sign1$;" if ch_num > 1 else ""
# ----------------------------------- CH2 -----------------------------------
port2 = '''
    input wire polor2,
    input wire [$tim_width-1$:0] cycle2,
'''
port2 = port2 if param2 and ch_num > 2 else ""
polor_sign2 = "polor2" if param2 else "PWM_PLOAR2"
local2 = '''
    localparam PWM_PLOAR2 = 1'b$int(polar2)$;
    localparam PWM_CYCLE2 = $tim_width$'d$cmp2$;
'''
local2 = local2 if not param2 and ch_num > 2 else ""
cycle_sign2 = "cycle2" if param2 else "PWM_CYCLE2"
assign2 = "oc[2] = (tim < $cycle_sign2$) ? $polor_sign2$ : ~$polor_sign2$;" if ch_num > 2 else ""
# ----------------------------------- CH3 -----------------------------------
port3 = '''
    input wire polor3,
    input wire [$tim_width-1$:0] cycle3,
'''
port3 = port3 if param3 else ""
polor_sign3 = "polor3" if param3 and ch_num > 3 else "PWM_PLOAR3"
local3 = '''
    localparam PWM_PLOAR3 = 1'b$int(polar3)$;
    localparam PWM_CYCLE3 = $tim_width$'d$cmp3$;
'''
local3 = local3 if not param3 and ch_num > 3 else ""
cycle_sign3 = "cycle3" if param3 else "PWM_CYCLE3"
assign3 = "oc[3] = (tim < $cycle_sign3$) ? $polor_sign3$ : ~$polor_sign3$;" if ch_num > 3 else ""

rco_code = '''
    /// 输出脉冲逻辑: rco
    reg [$rco_n-1$:0] rco_timer;
    always @(posedge clk or posedge rst) begin
        if (rst)
            rco_timer <= 0;
        else if (equal)  /// 触发rco计时计数器
            rco_timer <= $2**rco_n - 1$;
        else if (rco_timer > 0)
            rco_timer <= rco_timer - $f"{rco_n}'d1"$;
        else
            rco_timer <= 0;
    end

    assign rco = rco_timer != 0;
'''
rco_code = rco_code if port_rco else ""

code1 = '''
module PWM_$pwm_suffix$ (
    input wire clk,
    input wire rst,
    // #rst
    $"input wire en," if port_en else ""$
    $port0$$port1$$port2$$port3$
    
    output wire [$ch_num-1$:0] oc
    $",output wire rco" if port_rco else ""$
);
    localparam TIM_WIDTH = $tim_width$;
    localparam TIM_CMP = $max_cnt$;    // TIMER比较值
    $local0$$local1$$local2$$local3$
    // --

    reg [TIM_WIDTH-1:0] tim;
    always @(posedge clk or posedge rst) begin
        if (rst) begin
            tim <= 0;
        end else $"if (en)" if port_en else ""$begin
            if (tim == TIM_CMP) begin
                tim <= 0;
            end else begin
                tim <= tim + $tim_width$'b1;
            end
        end
    end

    $assign0$
    $assign1$
    $assign2$
    $assign3$

    $rco_code$

endmodule
'''

res_code = code1
*/


$res_code$

