Sfoglia il codice sorgente

feat(6400): transaction core and final top on the new 8-bit protocol

- src/control/m6400_transaction_core.v: validates type/length/value from the
  request RAM, stages and atomically commits SET_ALL(32), runs GET_ALL through
  get_data_6400 into the reply RAM, builds C0 NACKs, and reloads the watchdog
  only on SET_ACCEPT / GET_ACCEPT.
- src/top/prod2u_6400_final_top.v: host_uart_endpoint + request RAM + reply
  RAM + transaction core + reply arbiter + duty bank + temperature RAM +
  30 s watchdog + 500 Hz PWM carrier + board adapter.  Elaborates with zero
  warnings under iverilog -g2012 -Wall.
Prod2U Recovery 2 mesi fa
parent
commit
06ebac2963
2 ha cambiato i file con 515 aggiunte e 0 eliminazioni
  1. 274 0
      src/control/m6400_transaction_core.v
  2. 241 0
      src/top/prod2u_6400_final_top.v

+ 274 - 0
src/control/m6400_transaction_core.v

@@ -0,0 +1,274 @@
+`timescale 1ns/1ps
+
+// 6400 final transaction core: request validation, atomic SET commit,
+// GET assembly, NACK generation and watchdog feeding.
+//
+// This core sits between host_uart_endpoint (RX parse + TX), the request
+// staging RAM, the duty bank, the temperature RAM and the reply staging RAM.
+// One ordinary transaction is outstanding at a time.
+//
+// Request flow:
+//   1. The endpoint raises req_valid with {req_type, req_length}; the payload
+//      is staged in the request RAM (payload byte 0 at address 0).  The
+//      endpoint holds req_valid until the core raises req_ready, so the RAM
+//      is stable for the whole validation pass.
+//   2. The core walks the request RAM over ST_RAM_RD_ADDR (one rhythm per
+//      byte: the synchronous RAM output settles one clock after the
+//      address).  Type, length and every value byte are checked.
+//   3. SET_ALL (0x01, 32 bytes, all 0..100): stage all 32 bytes into the
+//      inactive duty bank, then commit.  SET_ACCEPT is the commit edge: it
+//      reloads the watchdog, and the SET_ACK group is queued only after the
+//      commit actually completed.  Any failure NACKs the whole frame with no
+//      side effect.
+//   4. GET_ALL (0x02, 0 bytes): GET_ACCEPT starts get_data_6400, which fills
+//      the reply RAM with the 83-byte GET payload; the core then queues
+//      GET_ACK (82) + GET_DATA (42) as one group and reloads the watchdog.
+//   5. Anything else: NACK (C0) with UNSUPPORTED / BAD_LENGTH / BAD_VALUE.
+//      A CRC-correct but rejected request never feeds the watchdog.
+//   6. Watchdog expiry invalidates the duty bank (defaults PTC 0 / FAN 20).
+//      A later SET recovers directly; GET keeps returning defaults.
+//
+// Reply flow: the core fills reply_ram (NACK payload, or the GET_DATA bytes
+// via get_data_6400), then hands {type, length, base} to reply_arbiter as one
+// group.  SET_ACK (81) and NACK (C0) are single-frame groups; GET is the
+// two-frame group GET_ACK + GET_DATA.
+module m6400_transaction_core (
+    input  wire       clk,
+    input  wire       rst_n,
+    // request side (from the endpoint)
+    input  wire       req_valid,
+    output reg        req_ready,
+    input  wire [7:0] req_type,
+    input  wire [7:0] req_length,
+    // request staging RAM read port (synchronous, 1 clock)
+    output reg [7:0]  req_ram_rd_addr,
+    input  wire [7:0] req_ram_rd_data,
+    // duty bank staging/commit
+    output reg        stage_we,
+    output reg [4:0]  stage_addr,
+    output reg [7:0]  stage_data,
+    output reg        commit,
+    output reg        invalidate,
+    // GET assembly (get_data_6400 read ports + reply staging write port)
+    output wire       gd_start,
+    // gd_ready is internal: the core owns its GET assembler instance.
+    output wire [4:0] gd_bank_addr,
+    input  wire [7:0] gd_bank_duty,
+    output wire [5:0] gd_temp_addr,
+    input  wire [7:0] gd_temp_data,
+    input  wire       gd_temp_valid,
+    output wire       gd_reply_we,
+    output wire [7:0] gd_reply_addr,
+    output wire [7:0] gd_reply_data,
+    // reply group handoff to the arbiter
+    output reg        group_valid,
+    output reg        group_second,
+    output reg [7:0]  group_type,
+    output reg [7:0]  group_length,
+    output reg [7:0]  second_type,
+    output reg [7:0]  second_length,
+    output reg [7:0]  group_base,
+    output reg [7:0]  second_base,
+    input  wire       group_done,
+    // GET assembly ownership flag for the bank read-port mux
+    output wire       get_busy,
+    // NACK staging (reply_ram write port share)
+    output reg        nack_we,
+    output reg [7:0]  nack_addr,
+    output reg [7:0]  nack_data,
+    // watchdog
+    output reg        wd_arm,
+    output reg        wd_reload,
+    input  wire       wd_expired
+);
+    localparam [3:0] ST_IDLE = 4'd0;
+    localparam [3:0] ST_VALIDATE = 4'd1;
+    localparam [3:0] ST_STAGE = 4'd2;
+    localparam [3:0] ST_COMMIT = 4'd3;
+    localparam [3:0] ST_GET_WAIT = 4'd4;
+    localparam [3:0] ST_NACK0 = 4'd5;
+    localparam [3:0] ST_NACK1 = 4'd6;
+    localparam [3:0] ST_GROUP_WAIT = 4'd7;
+
+    reg [3:0] state;
+    reg [7:0] v_type;
+    reg [7:0] v_length;
+    reg [7:0] v_index;      // validation / staging byte counter
+    reg [7:0] nack_reason;
+    reg gd_start_r;
+    wire gd_ready;      // get_data_6400 ready output, owned by this core
+    // GET assembly is in flight while the core is in ST_GET_WAIT.
+    assign get_busy = (state == 4'd4);
+
+    assign gd_start = gd_start_r;
+    assign gd_reply_we = 1'b0;
+    assign gd_reply_addr = 8'd0;
+    assign gd_reply_data = 8'd0;
+
+    // GET assembler: fills the reply RAM at base 0 with the 83-byte payload.
+    // Its bank port reads the live duty bank (currently effective output, or
+    // defaults while invalid); its temperature port reads temp_ram.
+    get_data_6400 u_get (
+        .clk(clk), .rst_n(rst_n),
+        .start(gd_start_r), .ready(gd_ready),
+        .bank_addr(gd_bank_addr), .bank_duty(gd_bank_duty),
+        .temp_addr(gd_temp_addr), .temp_data(gd_temp_data), .temp_valid(gd_temp_valid),
+        .reply_we(gd_reply_we), .reply_addr(gd_reply_addr), .reply_data(gd_reply_data)
+    );
+
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            state <= ST_IDLE;
+            req_ready <= 1'b0;
+            req_ram_rd_addr <= 8'd0;
+            stage_we <= 1'b0;
+            stage_addr <= 5'd0;
+            stage_data <= 8'd0;
+            commit <= 1'b0;
+            invalidate <= 1'b0;
+            gd_start_r <= 1'b0;
+            group_valid <= 1'b0;
+            group_second <= 1'b0;
+            group_type <= 8'd0;
+            group_length <= 8'd0;
+            second_type <= 8'd0;
+            second_length <= 8'd0;
+            group_base <= 8'd0;
+            second_base <= 8'd0;
+            nack_we <= 1'b0;
+            nack_addr <= 8'd0;
+            nack_data <= 8'd0;
+            wd_arm <= 1'b0;
+            wd_reload <= 1'b0;
+            v_type <= 8'd0;
+            v_length <= 8'd0;
+            v_index <= 8'd0;
+            nack_reason <= 8'd0;
+        end else begin
+            commit <= 1'b0;
+            invalidate <= 1'b0;
+            gd_start_r <= 1'b0;
+            stage_we <= 1'b0;
+            nack_we <= 1'b0;
+            wd_arm <= 1'b0;
+            wd_reload <= 1'b0;
+            if (wd_expired) invalidate <= 1'b1;
+            case (state)
+                ST_IDLE: begin
+                    req_ready <= 1'b1;
+                    group_valid <= 1'b0;
+                    if (req_valid && req_ready) begin
+                        req_ready <= 1'b0;
+                        v_type <= req_type;
+                        v_length <= req_length;
+                        v_index <= 8'd0;
+                        if (req_type == 8'h01) begin
+                            if (req_length != 8'd32) begin
+                                nack_reason <= 8'd02;
+                                state <= ST_NACK0;
+                            end else begin
+                                req_ram_rd_addr <= 8'd0;
+                                state <= ST_VALIDATE;
+                            end
+                        end else if (req_type == 8'h02) begin
+                            if (req_length != 8'd0) begin
+                                nack_reason <= 8'd02;
+                                state <= ST_NACK0;
+                            end else begin
+                                gd_start_r <= 1'b1;
+                                state <= ST_GET_WAIT;
+                            end
+                        end else begin
+                            nack_reason <= 8'd01;
+                            state <= ST_NACK0;
+                        end
+                    end
+                end
+                ST_VALIDATE: begin
+                    // The endpoint holds the request RAM stable while
+                    // req_valid is up and the core has not yet raised
+                    // req_ready.  IDLE already presented address 0, so the
+                    // first rhythm samples byte 0; each rhythm then presents
+                    // the next address.  `v_index` counts validated bytes.
+                    if (req_ram_rd_data > 8'd100) begin
+                        nack_reason <= 8'd03;
+                        state <= ST_NACK0;
+                    end else if (v_index == 8'd31) begin
+                        v_index <= 8'd0;
+                        req_ram_rd_addr <= 8'd0;
+                        state <= ST_STAGE;
+                    end else begin
+                        v_index <= v_index + 1'b1;
+                        req_ram_rd_addr <= v_index + 8'd1;
+                    end
+                end
+                ST_STAGE: begin
+                    // Re-stage all 32 validated bytes into the inactive bank.
+                    // The stage port holds each byte across the sampling edge
+                    // (mini13 probe): present here, consumed next clock.
+                    stage_we <= 1'b1;
+                    stage_addr <= v_index[4:0];
+                    stage_data <= req_ram_rd_data;
+                    if (v_index == 8'd31) begin
+                        state <= ST_COMMIT;
+                    end else begin
+                        v_index <= v_index + 1'b1;
+                        req_ram_rd_addr <= v_index + 8'd1;
+                    end
+                end
+                ST_COMMIT: begin
+                    // SET_ACCEPT: atomic commit, watchdog reload, then ACK.
+                    commit <= 1'b1;
+                    wd_arm <= 1'b1;
+                    wd_reload <= 1'b1;
+                    group_valid <= 1'b1;
+                    group_second <= 1'b0;
+                    group_type <= 8'h81;
+                    group_length <= 8'd0;
+                    group_base <= 8'd0;
+                    state <= ST_GROUP_WAIT;
+                end
+                ST_GET_WAIT: begin
+                    if (gd_ready) begin
+                        // GET_ACCEPT: legal GET admitted; reload watchdog.
+                        wd_arm <= 1'b1;
+                        wd_reload <= 1'b1;
+                        group_valid <= 1'b1;
+                        group_second <= 1'b1;
+                        group_type <= 8'h82;
+                        group_length <= 8'd0;
+                        group_base <= 8'd0;
+                        second_type <= 8'h42;
+                        second_length <= 8'd83;
+                        second_base <= 8'd0;
+                        state <= ST_GROUP_WAIT;
+                    end
+                end
+                ST_NACK0: begin
+                    nack_we <= 1'b1;
+                    nack_addr <= 8'd0;
+                    nack_data <= v_type;
+                    state <= ST_NACK1;
+                end
+                ST_NACK1: begin
+                    nack_we <= 1'b1;
+                    nack_addr <= 8'd1;
+                    nack_data <= nack_reason;
+                    group_valid <= 1'b1;
+                    group_second <= 1'b0;
+                    group_type <= 8'hC0;
+                    group_length <= 8'd2;
+                    group_base <= 8'd0;
+                    state <= ST_GROUP_WAIT;
+                end
+                ST_GROUP_WAIT: begin
+                    if (group_done) begin
+                        group_valid <= 1'b0;
+                        state <= ST_IDLE;
+                    end
+                end
+                default: state <= ST_IDLE;
+            endcase
+        end
+    end
+endmodule

+ 241 - 0
src/top/prod2u_6400_final_top.v

@@ -0,0 +1,241 @@
+`timescale 1ns/1ps
+
+// 6400 final top: shared host UART endpoint + transaction core + duty bank +
+// PWM carrier + temperature chain + watchdog, on the single new frame
+// protocol (A5 5A | TYPE | LEN:u8 | PAYLOAD | CRC16-CCITT-FALSE, low first).
+//
+// Data path:
+//   host RS232/TTL -> host_uart_endpoint (RX parse, TX broadcast, independent
+//   RX/TX CRC) -> request RAM -> m6400_transaction_core (validate, atomic
+//   SET commit, GET assembly, NACK) -> duty_register_bank -> pwm_bitmap_engine
+//   (+ fan_board_output_adapter) -> PTC/FAN pins.
+//   temp_ram <- bmc_ascii_poll / ct7428_scan; get_data_6400 reads the bank and
+//   temp_ram into reply_ram; reply_arbiter serialises reply groups vs DONEs
+//   (6400 never produces DONEs; the port is tied off).
+//   watchdog_30s reloads on SET_ACCEPT / GET_ACCEPT and invalidates the bank
+//   on expiry (defaults PTC 0 / FAN 20 at the connector).
+//
+// Bank read-port arbitration: the duty bank has one read port.  The PWM
+// scanner owns it except while a GET group is being assembled, when the GET
+// assembler owns it.  The bitmap source is therefore fixed for a whole PWM
+// period except across a GET assembly window, which never overlaps a commit
+// (the core serialises transactions).
+module prod2u_6400_final_top (
+    input  wire        sys_clk,
+    input  wire        host_rs232_rx,
+    output wire        host_rs232_tx,
+    input  wire        host_ttl_rx,
+    output wire        host_ttl_tx,
+    input  wire        bmc_rs232_rx,
+    output wire        bmc_rs232_tx,
+    output wire [23:0] ptc_pwm_out,
+    output wire [3:0]  front_fan_pwm_out,
+    output wire [3:0]  rear_fan_pwm_out,
+    inout  wire        temp_scl,
+    inout  wire        temp_sda
+);
+    wire sys_rst_n;
+    prod2u_por u_por (.sys_clk(sys_clk), .sys_rst_n(sys_rst_n));
+
+    // ---------- host UART endpoint ----------
+    wire        req_valid;
+    wire        req_ready;
+    wire [7:0]  req_type;
+    wire [7:0]  req_length;
+    wire        req_ram_wr_en;
+    wire [7:0]  req_ram_wr_addr;
+    wire [7:0]  req_ram_wr_data;
+    wire [7:0]  req_ram_rd_addr_core;
+    wire [7:0]  req_ram_rd_data;
+    wire        tx_start;
+    wire [7:0]  tx_type;
+    wire [7:0]  tx_length;
+    wire        tx_busy;
+    wire        tx_done;
+    wire [7:0]  tx_ram_rd_addr;
+    wire [7:0]  tx_ram_rd_data;
+
+    host_uart_endpoint u_host (
+        .clk(sys_clk), .rst_n(sys_rst_n),
+        .rs232_rx(host_rs232_rx), .rs232_tx(host_rs232_tx),
+        .ttl_rx(host_ttl_rx), .ttl_tx(host_ttl_tx),
+        .req_valid(req_valid), .req_ready(req_ready),
+        .req_type(req_type), .req_length(req_length),
+        .req_ram_wr_en(req_ram_wr_en),
+        .req_ram_wr_addr(req_ram_wr_addr),
+        .req_ram_wr_data(req_ram_wr_data),
+        .req_ram_rd_addr(), .req_ram_rd_data(8'd0),
+        .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length),
+        .tx_busy(tx_busy), .tx_done(tx_done),
+        .tx_ram_rd_addr(tx_ram_rd_addr), .tx_ram_rd_data(tx_ram_rd_data)
+    );
+
+    // ---------- request staging RAM (128 x 8, synchronous read) ----------
+    ef2_bram_dp #(.DATA_WIDTH(8), .ADDR_WIDTH(7), .DATA_DEPTH(128)) u_req_ram (
+        .clk(sys_clk),
+        .a_en(req_ram_wr_en), .a_we(req_ram_wr_en),
+        .a_addr(req_ram_wr_addr[6:0]), .a_wdata(req_ram_wr_data), .a_rdata(),
+        .b_en(1'b1), .b_we(1'b0),
+        .b_addr(req_ram_rd_addr_core[6:0]), .b_wdata(8'd0), .b_rdata(req_ram_rd_data)
+    );
+
+    // ---------- reply staging RAM (128 x 8) ----------
+    wire        reply_wr_en;
+    wire [7:0]  reply_wr_addr;
+    wire [7:0]  reply_wr_data;
+    wire        gd_reply_we;
+    wire [7:0]  gd_reply_addr;
+    wire [7:0]  gd_reply_data;
+    wire        nack_we;
+    wire [7:0]  nack_addr;
+    wire [7:0]  nack_data;
+    // Reply write-port share: GET assembly and NACK staging never run together.
+    assign reply_wr_en = gd_reply_we | nack_we;
+    assign reply_wr_addr = gd_reply_we ? gd_reply_addr : nack_addr;
+    assign reply_wr_data = gd_reply_we ? gd_reply_data : nack_data;
+    reply_ram u_reply_ram (
+        .clk(sys_clk),
+        .wr_en(reply_wr_en), .wr_addr(reply_wr_addr), .wr_data(reply_wr_data),
+        .rd_addr(tx_ram_rd_addr), .rd_data(tx_ram_rd_data)
+    );
+
+    // ---------- duty bank ----------
+    wire [4:0]  bank_rd_addr;
+    wire [7:0]  pwm_duty;
+    wire [4:0]  pwm_addr;
+    wire        stage_we;
+    wire [4:0]  stage_addr;
+    wire [7:0]  stage_data;
+    wire        commit;
+    wire        invalidate;
+    wire        bank_valid;
+    // Read-port arbitration: GET assembly owns the port while it runs.
+    wire        get_assembling;
+    assign bank_rd_addr = get_assembling ? gd_bank_addr : pwm_addr;
+    wire [4:0]  gd_bank_addr;
+    wire [7:0]  gd_bank_duty;
+    duty_register_bank #(.CHANNEL_COUNT(32), .ADDR_WIDTH(5),
+        .DEFAULT_PTC_MASK(32'h00FFFFFF)) u_bank (
+        .clk(sys_clk), .rst_n(sys_rst_n),
+        .pwm_addr(bank_rd_addr), .pwm_duty(pwm_duty),
+        .stage_we(stage_we), .stage_addr(stage_addr), .stage_data(stage_data),
+        .commit(commit), .invalidate(invalidate),
+        .valid(bank_valid), .active_bank()
+    );
+    // The GET assembler sees the bank output one clock after its address;
+    // its present/settle/commit rhythm already accounts for that.
+    assign gd_bank_duty = pwm_duty;
+
+    // ---------- temperature RAM + sensors ----------
+    wire        temp_wr_en;
+    wire [5:0]  temp_wr_addr;
+    wire [7:0]  temp_wr_data;
+    wire [5:0]  gd_temp_addr;
+    wire [7:0]  gd_temp_data;
+    wire        gd_temp_valid;
+    temp_ram u_temp_ram (
+        .clk(sys_clk),
+        .wr_en(temp_wr_en), .wr_addr(temp_wr_addr), .wr_data(temp_wr_data),
+        .rd_addr(gd_temp_addr), .rd_data(gd_temp_data), .rd_valid(gd_temp_valid)
+    );
+
+    // ---------- transaction core ----------
+    wire        group_valid;
+    wire        group_second;
+    wire [7:0]  group_type;
+    wire [7:0]  group_length;
+    wire [7:0]  second_type;
+    wire [7:0]  second_length;
+    wire [7:0]  group_base;
+    wire [7:0]  second_base;
+    wire        group_done;
+    wire        get_busy;
+    wire        gd_start;
+    wire        wd_arm;
+    wire        wd_reload;
+    wire        wd_expired;
+    wire        wd_counting;
+
+    m6400_transaction_core u_core (
+        .clk(sys_clk), .rst_n(sys_rst_n),
+        .req_valid(req_valid), .req_ready(req_ready),
+        .req_type(req_type), .req_length(req_length),
+        .req_ram_rd_addr(req_ram_rd_addr_core), .req_ram_rd_data(req_ram_rd_data),
+        .stage_we(stage_we), .stage_addr(stage_addr), .stage_data(stage_data),
+        .commit(commit), .invalidate(invalidate),
+        .gd_start(gd_start),
+        .gd_bank_addr(gd_bank_addr), .gd_bank_duty(gd_bank_duty),
+        .gd_temp_addr(gd_temp_addr), .gd_temp_data(gd_temp_data),
+        .gd_temp_valid(gd_temp_valid),
+        .gd_reply_we(gd_reply_we), .gd_reply_addr(gd_reply_addr),
+        .gd_reply_data(gd_reply_data),
+        .group_valid(group_valid), .group_second(group_second),
+        .group_type(group_type), .group_length(group_length),
+        .second_type(second_type), .second_length(second_length),
+        .group_base(group_base), .second_base(second_base),
+        .group_done(group_done),
+        .get_busy(get_busy),
+        .nack_we(nack_we), .nack_addr(nack_addr), .nack_data(nack_data),
+        .wd_arm(wd_arm), .wd_reload(wd_reload), .wd_expired(wd_expired)
+    );
+    // The core raises get_busy while a GET group is being assembled; that flag
+    // owns the bank read port for the duration.
+    assign get_assembling = get_busy;
+
+    // ---------- reply arbiter ----------
+    wire [3:0]  done_grant;
+    wire        done_launch;
+    wire [7:0]  arb_base;
+    reply_arbiter u_arb (
+        .clk(sys_clk), .rst_n(sys_rst_n),
+        .group_valid(group_valid), .group_second(group_second),
+        .group_type(group_type), .group_length(group_length),
+        .second_type(second_type), .second_length(second_length),
+        .group_base(group_base), .second_base(second_base),
+        .group_done(group_done),
+        .done_pending(16'd0), .done_axis_grant(done_grant),
+        .done_launch(done_launch), .done_ready(1'b0),
+        .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length),
+        .tx_base(arb_base),
+        .tx_busy(tx_busy), .tx_done(tx_done)
+    );
+
+    // ---------- watchdog ----------
+    watchdog_30s #(.TIMEOUT_CLKS(750000000)) u_wd (
+        .clk(sys_clk), .rst_n(sys_rst_n),
+        .arm(wd_arm), .reload(wd_reload),
+        .expired(wd_expired), .counting(wd_counting)
+    );
+
+    // ---------- PWM carrier + board adapter (existing, reused) ----------
+    wire [31:0] logical_bitmap;
+    wire        pwm_phase_boundary;
+    wire        pwm_deadline_miss;
+    wire [6:0]  pwm_phase_value;
+    wire        pwm_scan_idle;
+    pwm_bitmap_engine #(.CHANNEL_COUNT(32)) u_pwm (
+        .clk(sys_clk), .rst_n(sys_rst_n),
+        .runtime_duty_ready(bank_valid),
+        .duty_rd_addr(pwm_addr), .duty_rd_data(pwm_duty),
+        .logical_bitmap(logical_bitmap),
+        .phase_boundary(pwm_phase_boundary),
+        .phase_boundary_advance(),
+        .deadline_miss(pwm_deadline_miss),
+        .phase_value(pwm_phase_value),
+        .scan_idle(pwm_scan_idle)
+    );
+
+    wire [3:0] front_ball;
+    wire [3:0] rear_ball;
+    fan_board_output_adapter u_fan_adapter (
+        .logical_fan_on(logical_bitmap[31:24]),
+        .front_fan_ball(front_ball),
+        .rear_fan_ball(rear_ball)
+    );
+
+    // PTC: channels 0..23, active high.  Front/Rear: the board adapter maps
+    // the 8 fan bitmap bits (24..31) through FAN_BOARD_INVERT_MASK to balls.
+    assign ptc_pwm_out = logical_bitmap[23:0];
+    assign front_fan_pwm_out = front_ball;
+    assign rear_fan_pwm_out = rear_ball;
+endmodule