OpenCores
URL https://opencores.org/ocsvn/ds1621/ds1621/trunk

Subversion Repositories ds1621

[/] [ds1621/] [trunk/] [files/] [DS1621_b.sv] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 akhachat
// LTX-CREDENCE
2
// Project:   XXX-X
3
//
4
// Module:    DS1621_b
5
// Revision:  01
6
// Language:  SystemVerilog
7
//
8
// Engineer:  Ashot Khachatryan
9
// Function:  DS1621 temperature sensor. Behavioral model. Accesses the DS1621_b_nvm.sv memory file.
10
//
11
// Comments:  20091202 AKH: Created.  ADAPTED TO CADENCE IUS8.2
12
//            20091216 AKH: Has given up adding the features.
13
//                          DS1621_CNT (8'hA8) & DS1621_SLP (8'hA9) registers are not supported by this model.
14
//            20100402 AKH: Replaced the global temperature variable with real input (convenient for stand alone using/testing).
15
//
16
//            I couldn't find the model in the Internet. So, feel free to use it anywhere.
17
//
18
 
19
`timescale 1ns/10ps
20
 
21
`ifndef DS1621_STOP
22
  `define DS1621_STOP  8'h22
23
  `define DS1621_TH    8'hA1
24
  `define DS1621_TL    8'hA2
25
  `define DS1621_CNT   8'hA8
26
  `define DS1621_SLP   8'hA9
27
  `define DS1621_TMP   8'hAA
28
  `define DS1621_CFG   8'hAC
29
  `define DS1621_STRT  8'hEE
30
`endif
31
 
32
module DS1621_b(
33
     input         SCL
34
    ,inout         SDA
35
    ,input         A0
36
    ,input         A1
37
    ,input         A2
38
    ,output        TOUT
39
    ,input  [64:1] TEMP_R
40
);
41
 
42
parameter tsafe_sim    = 1000;
43
parameter NVM_WRITE_TM = 10_000_000;    // Write time
44
parameter NVM_WRITE_CP = 500;           // internal clock period
45
parameter TMP_CONV_TIM = 1_000_000_000; // Temperature conversion time (can be shortened for simulation in the module instance)
46
`define   DS1621_ID      4'h9
47
 
48
real        int_tmp_port;
49
real        int_tmp;
50
 
51
shortint    TH, TL, TMP;
52
reg         rst_n, rst_n_d;
53
reg         POL, ONE_SHOT, t_alarm;
54
reg  [15:0] TH_init, TL_init, word3_init;
55
reg         POL_init, ONE_SHOT_init;
56
reg  [15:0] nv_RAM [2:0];
57
reg  [15:0] SRi;
58
reg   [7:0] SRo, SLP, CNT;
59
reg   [7:0] cur_acc;
60
reg   [9:0] bit_cnt;
61
reg         selected, rw_op;
62
reg         ev_start, ev_stop, ev_TH, ev_TL;
63
reg         st_write1, st_write2, st_read1, st_read2, st_ack_sl, st_ack_ms, st_pend_memw;
64
reg         rst_ev_start, rst_sel, bcnt_strt, a_memwr;
65
reg         int_clk;
66
reg  [14:0] eewr_tmr;   //    'h4E20 =        'd20_000 = 10ms
67
reg  [20:0] tcnv_tmr;   // 'h1E_8480 = 'd2_000_000_000 = 1s
68
reg   [2:0] iic_sm, iic_smn;
69
reg   [2:0] eewr_sm, eewr_smn;
70
reg   [2:0] tcnv_sm, tcnv_smn;
71
reg   [1:0] byte_cnt, ev_start_r, ev_stop_r;
72
reg         SDA_r;
73
reg         a_STOP_r;
74
reg         THF, TLF;  // status bits
75
 
76
tri1        SDA;
77
wire  [2:0] A210;
78
wire        select;
79
wire        rst_bit_cnt, rst_start, rst_stop, rst_timer, rst_ttimer, rst_byte_cnt, rst_pend, rst_bcnt_strt, rst_a_stop;
80
wire        THF_reset, TLF_reset, rst_thf, rst_tlf;
81
wire  [7:0] stat;
82
wire        DONE, NVB;  // status bits
83
wire        iic_start, ee_start, tcnv_start, timer_done, ttimer_done, a_START_w;
84
wire        wrt_idle, wrt_wait, wrt_done, tcnv_idle, tcnv_wait, tcnv_done;
85
wire        a_TH, a_TL, a_CNT, a_SLP, a_TMP, a_CFG;
86
wire        byte_cmd, byte_one, byte_two, byte_thr, two_byte_cmd;
87
 
88
initial begin  int_clk = 0; forever int_clk = #(NVM_WRITE_CP/2) ~int_clk;  end  // internal clock for EEPROM / Temperature convertion operations
89
 
90
  // access units
91
assign a_STOP         =  SRi[7:0] == `DS1621_STOP;
92
assign a_TH           =  SRi[7:0] == `DS1621_TH;
93
assign a_TL           =  SRi[7:0] == `DS1621_TL;
94
assign a_CNT          =  SRi[7:0] == `DS1621_CNT;
95
assign a_SLP          =  SRi[7:0] == `DS1621_SLP;
96
assign a_TMP          =  SRi[7:0] == `DS1621_TMP;
97
assign a_CFG          =  SRi[7:0] == `DS1621_CFG;
98
assign a_STRT         =  SRi[7:0] == `DS1621_STRT;
99
  // conditions
100
assign A210           =  {A2, A1, A0};
101
assign select         =  ev_start & (SRi[7:1] == {`DS1621_ID, A210});
102
assign stat           =  {DONE, THF, TLF, NVB, 2'b00, POL, ONE_SHOT};
103
assign rst_bit_cnt    =  ev_stop | ((bcnt_strt | bit_cnt[9]) & ~SCL) | ~rst_n;
104
assign rst_bcnt_strt  = ~SCL | ~rst_n;
105
assign rst_start      =  rst_ev_start | ev_stop | ~rst_n;
106
assign rst_timer      =  wrt_done  | ~rst_n;
107
assign rst_ttimer     =  tcnv_done | ~rst_n;
108
assign rst_byte_cnt   =  bcnt_strt | ~rst_n; //rst_ev_start
109
assign rst_pend       = (rst_timer & st_pend_memw) | ~rst_n;
110
assign rst_sel        = (~ev_start_r[1] & ev_start_r[0]) | ev_stop | ~rst_n;
111
assign rst_stop       = &ev_stop_r[1:0] | ~rst_n;
112
assign rst_a_stop     =  a_START_w | ~rst_n;
113
assign rst_thf        =  THF_reset | ~rst_n_d;
114
assign rst_tlf        =  TLF_reset | ~rst_n_d;
115
assign a_START_w      =  a_STRT & selected & byte_one & bit_cnt[8] & ~SCL & ~rw_op;
116
assign iic_start      = (ev_start & ~SCL) | (bit_cnt[0] & selected);
117
assign ee_start       =  st_pend_memw & ev_stop;
118
assign tcnv_start     = (ONE_SHOT & a_START_w) | (~ONE_SHOT & tcnv_idle & ~a_STOP_r);
119
assign timer_done     =  wrt_wait  & (eewr_tmr == (NVM_WRITE_TM / NVM_WRITE_CP));
120
assign ttimer_done    =  tcnv_wait & (tcnv_tmr == (TMP_CONV_TIM / NVM_WRITE_CP));
121
assign byte_cmd       =  byte_cnt == 2'b00;
122
assign byte_one       =  byte_cnt == 2'b01;
123
assign byte_two       =  byte_cnt == 2'b10;
124
assign byte_thr       =  byte_cnt == 2'b11;
125
  // status bits
126
assign NVB            = ~wrt_idle;
127
assign DONE           =  tcnv_idle;
128
assign THF_reset      = ~rw_op & bit_cnt[8] & cur_acc[1] & byte_two & ~SCL & ~SRi[6];
129
assign TLF_reset      = ~rw_op & bit_cnt[8] & cur_acc[1] & byte_two & ~SCL & ~SRi[5];
130
 
131
  // Start
132
always @( negedge SDA, posedge rst_start )
133
    if ( rst_start )      ev_start <= 1'b0;
134
    else if ( SCL )       ev_start <= 1'b1;
135
 
136
always @( negedge SCL )  rst_ev_start <= bit_cnt[9] & ev_start;
137
  //
138
 
139
  // Stop
140
always @( posedge SDA, posedge rst_stop )
141
    if ( rst_stop )  ev_stop <= 1'b0;
142
    else if ( SCL )  ev_stop <= 1'b1;  // one int_clk period
143
 
144
always @( posedge int_clk, negedge rst_n )
145
    if ( ~rst_n )  ev_stop_r[1:0] <= 1'b0;
146
    else           ev_stop_r[1:0] <= {ev_stop_r[0], ev_stop};
147
  //
148
 
149
  // bit counter
150
always @( posedge SCL, posedge rst_bit_cnt )
151
    if ( rst_bit_cnt )       bit_cnt <=  10'h001;
152
    else                     bit_cnt <= {bit_cnt, 1'b0};
153
 
154
always @( posedge ev_start, posedge rst_bcnt_strt )  // reset after the start condition received
155
    if ( rst_bcnt_strt )  bcnt_strt <= 1'b0;
156
    else                  bcnt_strt <= 1'b1;
157
  //
158
 
159
  // byte counter
160
always @( negedge SCL, posedge rst_byte_cnt )
161
    if ( rst_byte_cnt )                byte_cnt <= 2'b00;
162
    else if ( bit_cnt[9] & selected )  byte_cnt <= byte_cnt +1;
163
 
164
  // EEPROM write timer
165
always @( posedge int_clk, posedge rst_timer )
166
    if ( rst_timer )      eewr_tmr <= 15'h0000;
167
    else if ( wrt_wait )  eewr_tmr <= eewr_tmr +1;
168
 
169
  // Temperature conversion timer
170
always @( posedge int_clk, posedge rst_ttimer )
171
    if ( rst_ttimer )      tcnv_tmr <= 21'h00_0000;
172
    else if ( tcnv_wait )  tcnv_tmr <= tcnv_tmr +1;
173
 
174
  // Operation control
175
always @( negedge SCL, posedge rst_sel )
176
    if ( rst_sel )                              rw_op <= 1'b1;
177
    else if ( bit_cnt[8] & ev_start & select )  rw_op <= SRi[0];  // wr=0, rd=1
178
 
179
always @( negedge SCL, posedge rst_sel )
180
    if ( rst_sel )                              selected <= 1'b0;
181
    else if ( bit_cnt[8] & ev_start & select )  selected <= 1'b1;
182
 
183
always @( posedge int_clk, negedge rst_n )
184
    if ( ~rst_n )  ev_start_r[1:0] <= 1'b0;
185
    else           ev_start_r[1:0] <= {ev_start_r[0], ev_start};
186
  //
187
 
188
  // Current resource being accessed
189
always @( negedge SCL, negedge rst_n )
190
    if ( ~rst_n )                                          cur_acc[7:0] <=  0;
191
    else if ( bit_cnt[8] & selected & ~rw_op & byte_one )  cur_acc[7:0] <= {a_STOP, a_TH, a_TL, a_CNT, a_SLP, a_TMP, a_CFG, a_STRT};
192
 
193
  // STOP command retention for not ONE_SHOT mode
194
always @( negedge SCL, posedge rst_a_stop )
195
    if ( rst_a_stop )       a_STOP_r <= 1'b0;
196
    else if ( cur_acc[7] )  a_STOP_r <= 1'b1;
197
 
198
  // Pending NV memory write flag
199
always @( negedge SCL, posedge rst_pend )
200
    if ( rst_pend )         st_pend_memw <= 0;
201
    else if ( bit_cnt[9] )  st_pend_memw <= a_memwr | st_pend_memw;
202
 
203
always @( negedge SCL, posedge rst_pend )
204
    if ( rst_pend )                    a_memwr <= 0;
205
    else if ( bit_cnt[8] & selected )  a_memwr <= (  ( byte_thr & ((cur_acc[6] & (SRi[15:0] != TH_init)) || (cur_acc[5] & (SRi[15:0] != TL_init))) )
206
                                                  || ( byte_two &   cur_acc[1] & (SRi[1:0] != word3_init[1:0]) )
207
                                                  ) & ~rw_op & bit_cnt[8];
208
  //
209
 
210
  // Slave ACK
211
always @( negedge SCL, negedge rst_n )
212
    if ( ~rst_n )                                 st_ack_sl <= 1'b0;
213
    else if ( bit_cnt[8] & (~rw_op | ev_start) )  st_ack_sl <= 1'b1;
214
    else                                          st_ack_sl <= 1'b0;
215
 
216
  // Master ACK
217
always @( negedge SCL, negedge rst_n )
218
    if ( ~rst_n )                               st_ack_ms <= 1'b0;
219
    else if ( bit_cnt[8] & rw_op & ~ev_start )  st_ack_ms <= 1'b1;
220
    else                                        st_ack_ms <= 1'b0;
221
 
222
  // Shift register: input
223
always @( posedge SCL, negedge rst_n )
224
    if ( ~rst_n )                        SRi[15:0] <=  16'h0000;
225
    else if ( ~st_ack_sl & ~st_ack_ms )  SRi[15:0] <= {SRi[14:0], SDA};
226
 
227
  // Shift register: output
228
always @(negedge SCL) // a_STOP, a_TH, a_TL, a_CNT, a_SLP, a_TMP, a_CFG, a_STRT
229
    if ( bit_cnt[9] )  SRo[7:0] <= cur_acc[6] ? (byte_cmd ? TH[15:8]  : TH[7:0])  :
230
                                   cur_acc[5] ? (byte_cmd ? TL[15:8]  : TL[7:0])  :
231
                                   cur_acc[2] ? (byte_cmd ? TMP[15:8] : TMP[7:0]) :
232
                                   cur_acc[1] ?  stat : 8'hff;
233
    else               SRo[7:0] <= {SRo[6:0], 1'b1};
234
 
235
// DS1621 registers
236
  // TH
237
always @( negedge SCL, negedge rst_n_d )
238
    if ( ~rst_n_d )                                                      TH[15:8] <= TH_init[15:8];
239
    else if ( ~rw_op & bit_cnt[8] & cur_acc[6] & byte_two & ~wrt_wait )  TH[15:8] <= SRi[7:0];
240
 
241
always @( negedge SCL, negedge rst_n_d )
242
    if ( ~rst_n_d )                                                      TH[7:0] <= TH_init[7:0];
243
    else if ( ~rw_op & bit_cnt[8] & cur_acc[6] & byte_thr & ~wrt_wait )  TH[7:0] <= SRi[7:0];
244
  //
245
  // TL
246
always @( negedge SCL, negedge rst_n_d )
247
    if ( ~rst_n_d )                                                      TL[15:8] <= TL_init[15:8];
248
    else if ( ~rw_op & bit_cnt[8] & cur_acc[5] & byte_two & ~wrt_wait )  TL[15:8] <= SRi[7:0];
249
 
250
always @( negedge SCL, negedge rst_n_d )
251
    if ( ~rst_n_d )                                                      TL[7:0] <= TL_init[7:0];
252
    else if ( ~rw_op & bit_cnt[8] & cur_acc[5] & byte_thr & ~wrt_wait )  TL[7:0] <= SRi[7:0];
253
  //
254
  // CFG status bits: POL, ONE_SHOT
255
always @( negedge SCL, negedge rst_n_d )
256
    if ( ~rst_n_d ) begin
257
        POL      <= POL_init;
258
        ONE_SHOT <= ONE_SHOT_init;
259
    end
260
    else if ( ~rw_op & bit_cnt[8] & cur_acc[1] & byte_two & ~wrt_wait ) begin
261
        POL      <= SRi[1];
262
        ONE_SHOT <= SRi[0];
263
    end
264
  // THF, TLF
265
always @( posedge ev_TH, posedge rst_thf )
266
    if ( rst_thf )  THF <= 1'b0;
267
    else            THF <= 1'b1;
268
  // TLF
269
always @( posedge ev_TL, posedge rst_tlf )
270
     if ( rst_tlf )  TLF <= 1'b0;
271
    else             TLF <= 1'b1;
272
  //
273
 
274
  // EEPROM write state machine
275
`define DS1621EE_IDLE   eewr_sm[0]
276
`define DS1621EE_WAIT   eewr_sm[1]
277
`define DS1621EE_DONE   eewr_sm[2]
278
`define NDS1621EE_IDLE  3'b001
279
`define NDS1621EE_WAIT  3'b010
280
`define NDS1621EE_DONE  3'b100
281
 
282
always @( posedge int_clk, negedge rst_n )
283
    if ( ~rst_n )  eewr_sm <= `NDS1621EE_IDLE;
284
    else           eewr_sm <=  eewr_smn;
285
 
286
always @( * ) begin
287
    eewr_smn = eewr_sm;
288
    casex( 1 )
289
      `DS1621EE_IDLE: if ( ee_start )   eewr_smn = `NDS1621EE_WAIT;
290
      `DS1621EE_WAIT: if ( timer_done ) eewr_smn = `NDS1621EE_DONE;
291
      `DS1621EE_DONE:                   eewr_smn = `NDS1621EE_IDLE;
292
      default:                          eewr_smn = `NDS1621EE_IDLE;
293
    endcase
294
end
295
 
296
assign wrt_idle = `DS1621EE_IDLE;
297
assign wrt_wait = `DS1621EE_WAIT;
298
assign wrt_done = `DS1621EE_DONE;
299
  //
300
 
301
  // Temperature conversion state machine
302
`define DS1621T_IDLE   tcnv_sm[0]
303
`define DS1621T_WAIT   tcnv_sm[1]
304
`define DS1621T_DONE   tcnv_sm[2]
305
`define NDS1621T_IDLE  3'b001
306
`define NDS1621T_WAIT  3'b010
307
`define NDS1621T_DONE  3'b100
308
 
309
always @( posedge int_clk, negedge rst_n )
310
    if ( ~rst_n )  tcnv_sm <= `NDS1621EE_IDLE;
311
    else           tcnv_sm <=  tcnv_smn;
312
 
313
always @( * ) begin
314
    tcnv_smn = tcnv_sm;
315
    casex( 1 )
316
      `DS1621T_IDLE: if ( tcnv_start )  tcnv_smn = `NDS1621T_WAIT;
317
      `DS1621T_WAIT: if ( ttimer_done ) tcnv_smn = `NDS1621T_DONE;
318
      `DS1621T_DONE:                    tcnv_smn = `NDS1621T_IDLE;
319
      default:                          tcnv_smn = `NDS1621T_IDLE;
320
    endcase
321
end
322
 
323
assign tcnv_idle = `DS1621T_IDLE;
324
assign tcnv_wait = `DS1621T_WAIT;
325
assign tcnv_done = `DS1621T_DONE;
326
  //
327
 
328
  // Temperature conversion, behavioral
329
initial assign int_tmp_port = $bitstoreal( TEMP_R );
330
 
331
always @( * )
332
    if ( int_tmp_port < -55.0 )       int_tmp = -55.0;
333
    else if ( int_tmp_port > 125.0 )  int_tmp = 125.0;
334
    else                              int_tmp = int_tmp_port;
335
 
336
shortint TMP_tmp;
337
always @( posedge tcnv_done, negedge rst_n_d )
338
    if ( ~rst_n_d )                      TMP[15:0] = 16'h1700;  // initial valkue is 23*C
339
    else begin
340
        TMP_tmp   = $rtoi(int_tmp);
341
        TMP[15:8] = TMP_tmp[7:0];
342
        //$display("After rtoi TMP_tmp=%0h TMP[15:0]=%0h", TMP_tmp, TMP[15:0]);
343
        if ( (TMP_tmp - int_tmp) != 0 )  TMP[7:0] = 8'h80;
344
        else                             TMP[7:0] = 8'h00;
345
        //$display("Final                  TMP[15:0]=%0h", TMP[15:0]);
346
    end
347
  //
348
 
349
  // Temperature alarm
350
assign ev_TH = TMP > TH;
351
assign ev_TL = TMP < TL;
352
 
353
always @( * )
354
    if ( ~rst_n_d )    t_alarm = 1'b0;
355
    else if ( ev_TH )  t_alarm = 1'b1;
356
    else if ( ev_TL )  t_alarm = 1'b0;
357
    else               t_alarm = t_alarm;
358
  //
359
 
360
  // Output generation
361
assign two_byte_cmd  = |cur_acc[6:5] | cur_acc[2];
362
assign SDA           =  st_ack_sl ? 1'b0 : selected & rw_op & ~st_ack_ms & (byte_one | (byte_two & two_byte_cmd)) ? SRo[7] : 1'bz;
363
assign TOUT          =  t_alarm ^~ POL;
364
 
365
  // NV memory read & write
366
always @( posedge wrt_done, negedge rst_n ) begin
367
    if ( ~rst_n ) begin
368
        $readmemh( "DS1621_b_nvm.sv", nv_RAM );
369
        if ( nv_RAM[0] == 16'hxxxx )  TH_init    = 16'h0000;
370
        else                          TH_init    = nv_RAM[0];
371
        if ( nv_RAM[1] == 16'hxxxx )  TL_init    = 16'h0000;
372
        else                          TL_init    = nv_RAM[1];
373
        if ( nv_RAM[2] == 16'hxxxx )  word3_init = 16'h0000;
374
        else                          word3_init = nv_RAM[2];
375
        POL_init      = word3_init[1];
376
        ONE_SHOT_init = word3_init[0];
377
    end
378
    else if ( wrt_done ) begin
379
        nv_RAM[0] =  TH;
380
        nv_RAM[1] =  TL;
381
        nv_RAM[2] = {14'h0000, POL, ONE_SHOT};
382
        $writememh( "DS1621_b_nvm.sv", nv_RAM );
383
    end
384
    //$display("TH=%h, TL=%h, POL=%0h, ONE_SHOT=%0h", TH_init, TL_init, POL_init, ONE_SHOT_init);
385
end
386
 
387
  // timing checks
388
initial begin
389
    rst_n   = 0;
390
    rst_n_d = 1;     // memory init signal
391
   #(tsafe_sim / 2)
392
    rst_n_d = 0;
393
   #(tsafe_sim / 2)
394
    rst_n   = 1;
395
    rst_n_d = 1;
396
end
397
 
398
specify
399
    specparam
400
      `ifdef DS1621_STANDARD
401
         tBUF    = 4700,  // Bus free time
402
         tHD_STA = 4000,  // SCL+ hold time: start condition [repeated]
403
         tLOW    = 4700,  // SCL- width
404
         tHIGH   = 4000,  // SCL+ width
405
         //tHD_DAT =    0,  // SDA to SCL+ hold  time
406
         tSU_STA = 4700,  // SCL+ to SDA setup time for repeated start
407
         tSU_DAT =  250,  // SDA to SCL+ setup time
408
         tSU_STO = 4000;  // SCL+ to SDA setup time
409
      `else
410
         tBUF    = 1300,  // Bus free time
411
         tHD_STA =  600,  // SCL+ hold time: start condition [repeated]
412
         tLOW    = 1300,  // SCL- width
413
         tHIGH   =  600,  // SCL+ width
414
         //tHD_DAT =    0,  // SDA to SCL+ hold  time
415
         tSU_STA =  600,  // SCL+ to SDA setup time for repeated start
416
         tSU_DAT =  100,  // SDA to SCL+ setup time
417
         tSU_STO =  600;  // SCL+ to SDA setup time
418
      `endif
419
    $width( posedge SDA &&& SCL, tBUF  );
420
    $width( negedge SCL,         tLOW  );
421
    $width( posedge SCL,         tHIGH );
422
    $hold ( negedge SDA, negedge SCL &&& rst_n, tHD_STA );
423
    $setup( posedge SCL, negedge SDA &&& rst_n, tSU_STA );
424
    $setup( SDA, posedge SCL         &&& rst_n, tSU_DAT );
425
    $setup( posedge SCL, posedge SDA &&& rst_n, tSU_STO );
426
endspecify
427
 
428
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.