simul_lwir160x120_vospi.v 21.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*!
 * <b>Module:</b> simul_lwir160x120_vospi
 * @file simul_lwir160x120_vospi.v
 * @date 2019-03-30  
 * @author Andrey FIlippov
 *     
 * @brief simulates FLIR Lepton 3.0 output over VoSPI
 *
 * @copyright Copyright (c) 2019 Elphel, Inc .
 *
 * <b>License </b>
 *
 * simul_lwir160x120_vospi.v is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * simul_lwir160x120_vospi.v is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/> .
 *
 * Additional permission under GNU GPL version 3 section 7:
 * If you modify this Program, or any covered work, by linking or combining it
 * with independent modules provided by the FPGA vendor only (this permission
 * does not extend to any 3-rd party modules, "soft cores" or macros) under
 * different license terms solely for the purpose of generating binary "bitstream"
 * files and/or simulating the code, the copyright holders of this Program give
 * you the right to distribute the covered work without those independent modules
 * as long as the source code for them is available from the FPGA vendor free of
 * charge, and there is no dependence on any encrypted modules for simulating of
 * the combined code. This permission applies to you if the distributed code
 * contains all the components and scripts required to completely simulate it
 * with at least one of the Free Software programs.
 */
`timescale 1ns/1ps

module  simul_lwir160x120_vospi # (
    parameter DATA_FILE =         "/data_ssd/nc393/elphel393/fpga-elphel/x393/input_data/pattern_160_120_14.dat",  //
    parameter WINDOW_WIDTH    =    160,  //
    parameter WINDOW_HEIGHT   =    120,  //
45 46
    parameter LWIR_GPIO_IN    = 4'b0000,
    
47 48
    parameter TELEMETRY       =      1,  // 0 - disabled, 1 - as header, 2 - as footer
    parameter FRAME_PERIOD    = 946969,  // 26.4 fps @25 MHz
49 50 51
    parameter SEGMENT_PERIOD  =  10000,  // 236742,  // 26.4 fps @25 MHz
    parameter SEGMENTS_SEQ    =      8,  // 12 With ITAR
    
52 53 54 55
    parameter FRAME_DELAY     =    100,  // mclk period to start first frame 1
    parameter MS_PERIOD       =     25   // ahould actually be 25000  
)(
    input          mclk,
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    input          mrst,  // active low 
    input          pwdn,  // active low
    input          spi_clk,
    input          spi_cs,
    output         spi_miso,
    input          spi_mosi,
    inout          gpio0,
    inout          gpio1,
    inout          gpio2,
    inout          gpio3, // may be used as segment sync (ready)
    input          i2c_scl,
    inout          i2c_sda,
    output         mipi_dp,   // not implemented
    output         mipi_dn,   // not implemented
    output         mipi_clkp, // not implemented
    output         mipi_clkn, // not implemented
    
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    // telemetry data
    input   [15:0] telemetry_rev,
    input   [31:0] telemetry_status,
    input   [63:0] telemetry_srev,
    input   [15:0] telemetry_temp_counts,
    input   [15:0] telemetry_temp_kelvin,
    input   [15:0] telemetry_temp_last_kelvin,
    input   [31:0] telemetry_time_last_ms,
    input   [15:0] telemetry_agc_roi_top,
    input   [15:0] telemetry_agc_roi_left,
    input   [15:0] telemetry_agc_roi_bottom,
    input   [15:0] telemetry_agc_roi_right,
    input   [15:0] telemetry_agc_high,
    input   [15:0] telemetry_agc_low,
    input   [31:0] telemetry_video_format //???
    );
    localparam OUT_BITS =        16;
    localparam PACKET_PIXELS =   80;
    localparam PACKET_HEADER =    2;
    localparam PACKET_WORDS =    PACKET_PIXELS + PACKET_HEADER;
    localparam SEGMENT_PACKETS = 60; // w/o telemetry
    localparam SEGMENT_PACKETS_TELEMETRY = SEGMENT_PACKETS + ((TELEMETRY > 0)? 1 : 0);
    localparam FRAME_SEGMENTS =   4;
96
    localparam FRAME_PACKETS =        SEGMENT_PACKETS * FRAME_SEGMENTS;
97
    localparam FRAME_PACKETS_FULL =   FRAME_PACKETS + ((TELEMETRY==0) ? 0 : 4);
98 99 100 101 102 103 104
    localparam FRAMES =               2; // 2 frames in a ping-pong buffer 
    localparam FRAME_WORDS =          FRAME_SEGMENTS * (SEGMENT_PACKETS + ((TELEMETRY>0)?1 : 0)) * PACKET_WORDS;
    localparam SEGMENT_PACKETS_FULL = SEGMENT_PACKETS + ((TELEMETRY>0)?1 : 0);
    localparam SEGMENT_WORDS =        SEGMENT_PACKETS_FULL * PACKET_WORDS;
    localparam SEGMENTS_MIN =     4;  // minimal segments ready to be able to read
    localparam SEGMENTS_MAX =     6;  // maximum segments ready before sync is lost
        
105
    
106
    wire rst = !mrst;
107 108 109
    
    reg  [OUT_BITS-1:0] sensor_data[0 : WINDOW_WIDTH * WINDOW_HEIGHT - 1]; // SuppressThisWarning VEditor - Will be assigned by $readmem
    reg  [OUT_BITS-1:0] packed_data[0: FRAMES * FRAME_WORDS -1];
110
    reg  [OUT_BITS-1:0] packet_bad [0: PACKET_WORDS-1];
111 112 113 114 115 116 117 118

    wire [160*16-1:0] telemetry_a;
    wire [160*16-1:0] telemetry_b;

    // registers for copiing data to packet array
    
//'0xe7319'
    reg            [19:0]  frame_dly_cntr; // delay till next frame start   
119
    reg            [19:0]  segment_dly_cntr; // delay till next frame segment
120
    reg                    frame_start;
121 122 123 124
    reg                    segment_start;
    reg            [ 3:0]  segments_cntr;
    wire           [ 3:0]  segment_id;
    
125 126 127
    reg                    copy_page;
    reg                    copy_run;
    wire                   copy_done;
128 129
    reg                    segment_run;
    wire                   segment_done;
130 131
    
//    reg              [2:0] copy_segment; // 4 - copy average and telemetry
132 133 134
    reg              [7:0] copy_packet;       // number of a packet in a frame 240 image packets, then telemetry
    reg              [6:0] segment_packet;    // 60/61 packets
    
135 136 137 138
    reg              [6:0] copy_word;      // word number to copy in a packet (0..82), last one copies CRC to word 1
    reg                    copy_crc;
    wire             [7:0] copy_packet_full;
    wire             [7:0] copy_telemetry_packet; // only 2 LSB
139
//    wire             [7:0] copy_packet_segment;
140
    wire            [11:0] copy_packet_indx; // high bits are always 0
141
    wire             [3:0] copy_packet_ttt;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    
    reg             [15:0] copy_pxd;
    reg             [30:0] frame_sum;
    wire            [31:0] frame_average32;
    wire            [15:0] frame_average;
    reg             [15:0] copy_telemetry_d;
    reg             [15:0] copy_d;
    wire                   copy_pix_or_tel;
    wire                   copy_pix_only;
    wire                   copy_tel_only;
//    reg                    copy_pixels_r;
    reg                    copy_pixels_pix;
//    reg                    copy_pixels_tel;
    
    wire            [15:0] copy_din;
    wire            [ 6:0] copy_wa; // address in a packet where to write data
158
    wire            [15:0] copy_wa_full;
159 160 161 162 163 164 165
    wire            [15:0] crc_in;
    wire            [15:0] crc_out;
    reg                    en_avg; // write frame average value to telemetry
    reg             [31:0] frame_num;
    reg             [31:0] time_ms;
    reg             [31:0] ms_cntr;
    
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    //----------------
    wire                   start_segm_rd;      // starting readout page, spi_clk domain
    wire                   start_segm_rd_mclk; // starting readout page, resync to mclk
    reg                    readout_page;          // readout page number
    reg             [ 1:0] readout_segment;    // actually just 2 (2 bits, but will use FRAME_SEGMENTS==4
    reg             [ 5:0] readout_packet;     // number of packet read out in a segment
    reg             [ 6:0] readout_word_indx;  // number of word in a readout packet   
    reg             [ 3:0] readout_bit;        // 0 - msb, 15 - lsb
    reg             [15:0] readout_sr_good;
    reg             [15:0] readout_sr_bad;
    reg             [ 3:0] segments_ready;
    reg                    segment_av;            // segments avalable for readout
    reg                    sync_lost; 
    reg                    packet_sent;
//    reg                    packet_skipped; // sent invalid packet 
    reg                    packet_re_last;
    
    wire                   readout_last_segment;  // reading last segment in a frame
    wire                   readout_last_packet;   // reading last packet in a segment
    wire                   readout_last_word;     // reading last word in a packet
    wire                   readout_pre_last_bit;  // reading out pre-last bit
    wire                   readout_pre_first_bit; // reading out pre-first bit in a word
    reg                    readout_last_bit;     
    reg                    readout_first_bit = 1;
    wire            [15:0] readout_address;      // buffer readout address
    wire                   readout_good_w ;       // should be valid at first negative transition of the spi_clk
    reg                    readout_good;          // reading out/sending valid packet
    
        
    assign readout_last_segment =   readout_segment == (FRAME_SEGMENTS - 1);
    assign readout_last_packet =    readout_packet == (SEGMENT_PACKETS_TELEMETRY -1);
    assign readout_last_word =      readout_word_indx == (PACKET_WORDS -1);
    assign readout_pre_last_bit =   readout_bit[3:0] == 14;
    assign readout_pre_first_bit =  readout_bit[3:0] == 15;
    
    
    assign readout_address = readout_word_indx +
                            (PACKET_WORDS *  readout_packet) +
                            (SEGMENT_WORDS * readout_segment) +
                            (FRAME_WORDS *   readout_page);
                            
    assign readout_good_w = segment_av;                            
    assign spi_miso = readout_good ? readout_sr_good[15] : readout_sr_bad[15];
    assign start_segm_rd = (readout_word_indx == 0) && readout_last_bit && !spi_cs; 
    
211 212 213
//    wire            [ 2:0] copy_segment;

//946,969 '0xe7319'
214 215 216 217 218 219 220 221 222 223 224 225
      assign mipi_dp = 'bz;
      assign mipi_dn = 'bz;
      assign mipi_clkp = 'bz;
      assign mipi_clkn = 'bz;
      
      assign gpio0 = LWIR_GPIO_IN[0]?'bz: 0;
      assign gpio1 = LWIR_GPIO_IN[1]?'bz: 0;
      assign gpio2 = LWIR_GPIO_IN[2]?'bz: 0;
      assign gpio3 = LWIR_GPIO_IN[3]?'bz: 0;
      assign i2c_sda = 'bz;
      
      
226
      assign copy_done =           copy_run && copy_crc && (copy_packet== (FRAME_PACKETS_FULL -1));
227 228
      assign segment_done =        copy_run && copy_crc && (segment_packet== (SEGMENT_PACKETS_FULL -1));
      assign segment_id =          (segments_cntr < 4) ? (segments_cntr +1) : 4'b0;
229 230 231 232 233 234 235
      
      assign copy_packet_full =    (copy_packet < FRAME_PACKETS)?(copy_packet + ((TELEMETRY == 1) ? 4 : 0)):(copy_packet - ((TELEMETRY == 1)?FRAME_PACKETS: 0)) ;
      
      assign copy_telemetry_packet = copy_packet - FRAME_PACKETS;      
      assign copy_pix_or_tel =     copy_run && (copy_word < PACKET_PIXELS); //  && (copy_packet < FRAME_PACKETS);
      assign copy_pix_only =       copy_pix_or_tel && (copy_packet < FRAME_PACKETS);
      assign copy_tel_only =       copy_pix_or_tel && (copy_packet >= FRAME_PACKETS);
236
//      assign copy_packet_segment = copy_packet_full / SEGMENT_PACKETS_TELEMETRY;  /// 
237
      assign copy_packet_indx =    copy_packet_full % SEGMENT_PACKETS_TELEMETRY; 
238 239 240
      
      assign copy_packet_ttt =     (copy_packet_indx == 20) ? segment_id: 4'b0; 
      
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      assign crc_in =   (copy_word == 0) ? {4'b0,copy_packet_indx[11:0]}:(
                        (copy_word == 1) ?16'b0:copy_d[15:0]);

      assign copy_din = (copy_word == 0) ? {1'b0,copy_packet_ttt[2:0], copy_packet_indx[11:0]}:(
                        (copy_word == 1) ?16'bxx:(copy_crc? (crc_out): copy_d[15:0]));
                        
      assign copy_wa = copy_crc? 7'h01 : copy_word;                  

      assign copy_wa_full = copy_wa + (PACKET_WORDS * copy_packet_full) + (FRAME_WORDS * copy_page);
      
      assign frame_average32 = (frame_sum / (WINDOW_WIDTH * WINDOW_HEIGHT));
      assign frame_average = frame_average32[15:0];
      
`ifndef ROOTPATH
    `include "IVERILOG_INCLUDE.v"// SuppressThisWarning VEditor - maybe not used
    `ifndef ROOTPATH
        `define ROOTPATH "."
    `endif
`endif
260
integer i;
261 262
initial begin
// $readmemh({`ROOTPATH,"/input_data/sensor_16.dat"},sensor_data);
263 264 265 266 267 268 269
  $readmemh(DATA_FILE,sensor_data,0);
  //    reg  [OUT_BITS-1:0] packet_bad [0: PACKET_WORDS-1];
  packet_bad[0] <= 'h0f00;
  packet_bad[1] <= 'h0000; // calculate and put crc?
  for (i = 2; i < PACKET_WORDS; i = i+1) begin
    packet_bad[i] <= 0;
  end
270 271
end
always @ (posedge mclk) begin
272
    if (rst || (ms_cntr == 0)) ms_cntr <= MS_PERIOD -1;
273
    else                       ms_cntr <=  ms_cntr - 1;
274

275 276
    if      (rst)              time_ms <= 0;
    else if ((ms_cntr == 0))   time_ms <=  time_ms + 1;
277 278

    //restarting frames
279 280 281 282 283 284 285 286 287 288 289 290 291 292
    if      (rst)              segment_dly_cntr <= FRAME_DELAY;
    else if (segment_start)    segment_dly_cntr <= SEGMENT_PERIOD;
    else                       segment_dly_cntr <= segment_dly_cntr - 1;
    segment_start <=  !rst && (segment_dly_cntr == 0);
    
    if      (rst || segment_done) segment_run <= 0;
    else if (segment_start)       segment_run <= 1;
    
    
    
    if      (rst)              frame_dly_cntr <= FRAME_DELAY;
    else if (frame_start)      frame_dly_cntr <= FRAME_PERIOD;
    else                       frame_dly_cntr <= frame_dly_cntr - 1;
    
293
    
294
    frame_start <= !rst && (frame_dly_cntr == 0);
295
    
296 297
    if      (rst)              frame_num <= 0;
    else if (frame_start)      frame_num <=  frame_num + 1;
298
    
299 300
    if      (rst)              copy_page <= 0;
    else if (frame_start)      copy_page <= !copy_page;
301

302
    if      (rst || copy_done) copy_run <= 0;
303
    else if (frame_start)      copy_run <= 1;
304 305 306 307 308
    
    copy_crc <= copy_word == (PACKET_WORDS - 1); 
    if (!copy_run || copy_crc) copy_word <= 0;
    else                       copy_word <= copy_word + 1;
    
309 310 311 312 313 314 315 316 317 318 319 320 321
    if      (!copy_run)        copy_packet <= 0;
    else if (copy_crc)         copy_packet <= copy_packet + 1;
    
//    if       (rst || frame_start)  segment_packet <= 0;
//    else if (copy_run && copy_crc) segment_packet <= (copy_packet== (FRAME_PACKETS_FULL -1))? 0:  (segment_packet + 1);

    if         (rst || segment_start) segment_packet <= 0;
    else if (segment_run && copy_crc) segment_packet <= segment_packet + 1;

    
    if (rst)                  segments_cntr <= 0;
    else if (segment_done)    segments_cntr <= (segments_cntr == (SEGMENTS_SEQ - 1)) ? 0: (segments_cntr + 1);
    
322
    
323 324
    if (copy_pix_only)         copy_pxd <= sensor_data[copy_packet * PACKET_PIXELS + copy_word];
    else                       copy_pxd <= 'bx;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

    if (copy_tel_only)   copy_telemetry_d <= copy_telemetry_packet[1]?
                                             telemetry_b[(PACKET_PIXELS * (2 - copy_telemetry_packet[0])-copy_word - 1)*16 +: 16]:
                                             telemetry_a[(PACKET_PIXELS * (2 - copy_telemetry_packet[0])-copy_word - 1)*16 +: 16];
    else                 copy_telemetry_d <= 'bx;
    
    copy_d <= (copy_packet < FRAME_PACKETS) ? copy_pxd : copy_telemetry_d;
    
    copy_pixels_pix <= copy_pix_or_tel && (copy_packet < FRAME_PACKETS); 
    
    if      (frame_start)   frame_sum <= 0;
    else if (copy_pixels_pix) frame_sum <= frame_sum + copy_pxd;
    
    if (copy_run) packed_data[copy_wa_full] <= copy_din; // copy_d;
    
    en_avg <= copy_crc && (copy_packet == (FRAME_PACKETS - 1)); // 1 cycle after last pixel written
341 342 343 344 345 346 347
end

// readout, mclk part
always @ (posedge mclk) begin
    if      (rst)                                  segments_ready <= 0;
    else if ( segment_done && !start_segm_rd_mclk) segments_ready <= segments_ready + 1;
    else if (!segment_done &&  start_segm_rd_mclk) segments_ready <= segments_ready - 1;
348
    
349 350 351
    segment_av <= !rst && !sync_lost && (segments_ready >= SEGMENTS_MIN);
    if      (rst)                             sync_lost <= 0;
    else if (segments_ready >= SEGMENTS_MAX)  sync_lost <= 1; 
352 353
end

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
// readout, spi_clk part
always @ (posedge rst or negedge spi_clk) begin

    if      (rst)       readout_bit <= 0;
    else if (!spi_cs)   readout_bit <= readout_bit + 1;

    if      (rst)     readout_last_bit <= 0; 
    else if (!spi_cs) readout_last_bit <= readout_pre_last_bit;

    if      (rst)     readout_first_bit <= 1; 
    else if (!spi_cs) readout_first_bit <= readout_pre_first_bit;

    if      (rst)                                                      readout_good <= 0; 
    else if (!spi_cs && readout_first_bit && (readout_word_indx == 0)) readout_good <= readout_good_w;
    
    if (!spi_cs) begin
        if (readout_first_bit) begin
            readout_sr_good <= packed_data[readout_address];
            readout_sr_bad <=  packet_bad[readout_word_indx];
        end else begin
            readout_sr_good <= {readout_sr_good[14:0], 1'b0};
            readout_sr_bad <=  {readout_sr_bad[14:0],  1'b0};
        end
    end

    if      (rst)                         readout_word_indx <= 0;
    else if (!spi_cs && readout_last_bit) readout_word_indx <=packet_re_last ? 0 : (readout_word_indx + 1);
    
    if      (rst)   begin
        packet_re_last <= 0;
        packet_sent    <= 0;
//        packet_skipped <= 0;   
    end else if (!spi_cs)  begin
        packet_re_last <= readout_last_word && readout_pre_last_bit;
        packet_sent <=    readout_last_word && readout_pre_last_bit &&  readout_good;
//        packet_skipped <= readout_last_word && readout_pre_last_bit && !readout_good;
    end

    if      (rst)                    readout_packet <= 0;
    else if (!spi_cs && packet_sent) readout_packet <= readout_last_packet ? 0 : (readout_packet + 1);
    
    if      (rst)                                          readout_segment <= 0;
    else if (!spi_cs && packet_sent&& readout_last_packet) readout_segment <= readout_last_segment ? 0 : (readout_segment + 1);

    if      (rst)                                                                   readout_page <= 0;
    else if (!spi_cs && packet_sent && readout_last_packet && readout_last_segment) readout_page <= !readout_page;

end


404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

    crc16_x16x12x5x0 crc16_x16x12x5x0_i (
        .clk    (mclk), // input
        .srst   (!copy_run || copy_crc), // input
        .en     (1'b1),                  // input
        .din    (crc_in), // input[15:0] 
        .dout   (crc_out) // output[15:0]
         
    );


    simul_lwir160x120_telemetry simul_lwir160x120_telemetry_i (
        .clk                       (mclk),                       // input
        .en                        (frame_start),                // input
        .en_avg                    (en_avg),                     // input
        .telemetry_rev             (telemetry_rev),              // input[15:0] 
        .telemetry_time            (time_ms),                    // input[31:0] 
        .telemetry_status          (telemetry_status),           // input[31:0] 
        .telemetry_srev            (telemetry_srev),             // input[63:0] 
        .telemetry_frame           (frame_num),                  // input[31:0] 
        .telemetry_mean            (frame_average),              // input[15:0] 
        .telemetry_temp_counts     (telemetry_temp_counts),      // input[15:0] 
        .telemetry_temp_kelvin     (telemetry_temp_kelvin),      // input[15:0] 
        .telemetry_temp_last_kelvin(telemetry_temp_last_kelvin), // input[15:0] 
        .telemetry_time_last_ms    (telemetry_time_last_ms),     // input[31:0] 
        .telemetry_agc_roi_top     (telemetry_agc_roi_top),      // input[15:0] 
        .telemetry_agc_roi_left    (telemetry_agc_roi_left),     // input[15:0] 
        .telemetry_agc_roi_bottom  (telemetry_agc_roi_bottom),   // input[15:0] 
        .telemetry_agc_roi_right   (telemetry_agc_roi_right),    // input[15:0] 
        .telemetry_agc_high        (telemetry_agc_high),         // input[15:0] 
        .telemetry_agc_low         (telemetry_agc_low),          // input[15:0] 
        .telemetry_video_format    (telemetry_video_format),     // input[31:0] 
        .telemetry_a               (telemetry_a),                // output[2559:0] reg 
        .telemetry_b               (telemetry_b)                 // output[2559:0] reg 
    );

440 441 442 443 444 445 446 447
       pulse_cross_clock pulse_cross_clock_start_segm_i (
            .rst         (rst),                  // input extended to include sensor reset and rst_mmcm
            .src_clk     (!spi_clk),             // input
            .dst_clk     (mclk),                 // input
            .in_pulse    (start_segm_rd),                  // input
            .out_pulse   (start_segm_rd_mclk),              // output
            .busy() // output
        );
448 449 450 451 452


endmodule


453 454


455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
/*
// Most significant bit first (big-endian)
  // x^16+x^12+x^5+1 = (1) 0001 0000 0010 0001 = 0x1021
  function crc(byte array string[1..len], int len) {
     rem := 0
     // A popular variant complements rem here
      for i from 1 to len {
         rem  := rem xor (string[i] leftShift (n-8))   // n = 16 in this example
          for j from 1 to 8 {   // Assuming 8 bits per byte
              if rem and 0x8000 {   // if leftmost (most significant) bit is set
                 rem  := (rem leftShift 1) xor 0x1021
             } else {
                 rem  := rem leftShift 1
             }
             rem  := rem and 0xffff      // Trim remainder to 16 bits
         }
     }
     // A popular variant complements rem here
      return rem
 }

*/