interfacing and memoryoverview i repeat fsmd (for the vending machine) i interfaces i memory (intern...

52
Interfacing and Memory Martin Schoeberl Technical University of Denmark Embedded Systems Engineering April 23, 2020 1 / 52

Upload: others

Post on 10-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Interfacing and Memory

Martin Schoeberl

Technical University of DenmarkEmbedded Systems Engineering

April 23, 2020

1 / 52

Page 2: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Overview

I Repeat FSMD (for the vending machine)I InterfacesI Memory (intern and extern)I Serial interface (RS 232)I 2 hours lab SRAM exercise

I Exercise description is in DTU InsideI sram exercise.pdfI SRAM data sheet: CYCC1041...

I The course evaluation is open for feedback

2 / 52

Page 3: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

The Online Exam

I We will use the “old” online systemI Show it at http://onlineeksamen.dtu.dk/I It will be two parts:

I Multiple choiceI Download and hand in a document (e.g., PDF generated

from Word)I You can play with the very short one todayI We will do a test exam next week, before the lab

3 / 52

Page 4: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Using an FSM and a Datapath

I About the design of the vending machine (VM)I Some of you start coding the VM directly

I This may work for small designsI But does not scale

I Better use a systematic approachI Use a FSM that communicates with a datapath (FSMD)I We will quickly repeat FSMD

4 / 52

Page 5: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Finite-State Machine (FSM)

I Has a register that contains the stateI Has a function to computer the next state

I Depending on current state and inputI Has an output depending on the stateI Use a Moore FSM for the VM

5 / 52

Page 6: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Basic Finite-State Machine

I A state registerI Two combinational blocks

in

state

nextStateNext statelogic

Ouputlogic out

6 / 52

Page 7: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

State Diagram

bad event

green orange red/ring bell

bad event

clear

reset

clear

I States and transitions depending on input valuesI Example is a simple alarm FSMI Nice visualizationI Draw the state diagram for your VM during the designI Include a state diagram in the report

7 / 52

Page 8: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

The Input and Output of the Alarm FSM

I Two inputs and one output

val io = IO(new Bundle{

val badEvent = Input(Bool())

val clear = Input(Bool())

val ringBell = Output(Bool())

})

8 / 52

Page 9: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Encoding the State

I We can optimize state encodingI Two common encodings are: binary and one-hotI We leave it to the synthesize toolI Use symbolic names with an EnumI Note the number of states in the Enum constructI We use a Scala list with the :: operator

val green :: orange :: red :: Nil = Enum(3)

9 / 52

Page 10: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Start the FSM

I We have a starting state on reset

val stateReg = RegInit(green)

10 / 52

Page 11: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

The Next State Logicswitch (stateReg) {

is (green) {

when(io.badEvent) {

stateReg := orange

}

}

is (orange) {

when(io.badEvent) {

stateReg := red

} .elsewhen(io.clear) {

stateReg := green

}

}

is (red) {

when (io.clear) {

stateReg := green

}

}

}

11 / 52

Page 12: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

The Output Logic

io.ringBell := stateReg === red

12 / 52

Page 13: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Summary on the Alarm Example

I Three elements:1. State register2. Next state logic3. Output logic

I This was a so-called Moore FSMI There is also an FSM type called Mealy machine

13 / 52

Page 14: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

FSM with Datapath

I A type of computing machineI Consists of a finite-state machine (FSM) and a datapathI The FSM is the master (the controller) of the datapathI The datapath has computing elements

I E.g., adder, incrementer, constants, multiplexers, ...I The datapath has storage elements (registers)

I E.g., sum of money payed, count of something, ...I You VM design shall be a FSMD

14 / 52

Page 15: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

FSM-Datapath Interaction

I The FSM controls the datapathI For example, add 2 to the sum

I By controlling multiplexersI For example, select how much to addI Not adding means selecting 0 to add

I Which value goes whereI The FSM logic also depends on datapath output

I Is there enough money payed to release a can of soda?I FSM and datapath interact

15 / 52

Page 16: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Popcount Example

I An FSMD that computes the popcountI Also called the Hamming weightI Compute the number of ‘1’s in a wordI Input is the data wordI Output is the count

16 / 52

Page 17: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Popcount Block Diagram

dinValid popCntValid

FSM

din popCnt

popCntReadydinReady

Datapath

17 / 52

Page 18: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

The FSM

Count

Idle

Done

Valid

Finished

Result read

I A Very Simple FSMI Two transitions depend on input/output handshakeI One transition on the datapath output

18 / 52

Page 19: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

The Datapath

+

shfdin

00 cnt

count

19 / 52

Page 20: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Let’s Explore the Code

I In PopCount.scala

20 / 52

Page 21: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Usage of an FSMD

I Of course for your VMI The VM is a simple processorI But not Turing completeI Can only process coins of 2 and 5

I An FSMD can be used to build a processorI Fine for simple processorsI E.g., LipsiI Pipelined processor topic of

I Computer Architecture Engineering (02155)

21 / 52

Page 22: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Use a FSMD for the VM

I This is the main part your vending machineI Can be design and tested just with Chisel testers (no

FPGA board needed)I See the given tester

I Sets the price to 7I Adds two coins (2 and 5)I Presses the buy button

I Extend the test along the developmentI Remember test driven development?I Maybe write the test before the implementationI Maybe test developer and FSMD developer are not always

the same person

22 / 52

Page 23: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Code Snippets for the VM

val idle :: add2 ... :: Nil = Enum(?)

val stateReg = RegInit(idle)

...

switch (stateReg) {

is (idle){

when(coin2) {

stateReg := ...

}

when(...) {

...

switch(stateReg) {

is (add2) { ... } // drive the datapath for

adding a coin of kr. 2

23 / 52

Page 24: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Memory

I Registers are storage elements == memoryI Just use a Reg of a VecI This is 1 KiB of memory

val memoryReg = Reg(Vec(1024, UInt(8.W)))

// writing into memory

memoryReg(wrAddr) := wrData

// reading from the memory

val rdData = memoryReg(rdAddr)

I Simple, right?I But is this a good solution?

24 / 52

Page 25: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

A Flip-Flop

I Remember the circuit of a register (flip-flop)?I Two latches: master and slaveI One (enable) latch can be built with 4 NAND gatesI a NAND gate needs 6 transistors, an inverter 2 transistorsI A flip-flop needs 20 transistors (for a single bit)I Can we do better?

25 / 52

Page 26: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

A Memory Cell

I A single bit can be stored in 6 transistorsI That is how larger memories are builtI FPGAs have this type of on-chip memoriesI Usually many of them in units of 2 KiB or 4 KiBI We need some Chisel code to represent itI More memory needs an extra chipI Then we need to interface this memory from the FPGA

26 / 52

Page 27: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

SRAM Memory

I RAM stands for random access memoryI SRAM stands for static RAMI There is also something called DRAM for dynamic RAM

I Uses a capacitor and a transistorI DRAM is smaller than SRAMI But needs refreshesI Different technology than technology for logic

I All on-chip memory is SRAM (today)

27 / 52

Page 28: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Memory Interface

I InterfaceI Address input (e.g., 10 bits for 1 KiB)I Write signal (e.g., we)I Data inputI Data output

I May share pins for the data input and output (tri-state)I May have read and write address

I A so-called dual ported memoryI Can do a read and a write in the same clock cycle

28 / 52

Page 29: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

On-Chip Memory

I SRAM by itself is asynchronousI No clock, just the correct timingI Apply the address and after some time the data is validI But one can add input registers, which makes it a

synchronous SRAMI Current FPGAs have only synchronous memoriesI FPGAs usually have dual-ported memoriesI This means the result of a read is available on clock cycle

after the address is givenI This is different from the use of flip-flops (Reg(Vec(..)))

29 / 52

Page 30: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Synchronous Memory

rdAddr

wrAddr

wrData

rdData

wrEna

Memory

30 / 52

Page 31: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Use of a Chisel SyncReadMem

class Memory() extends Module {

val io = IO(new Bundle {

val rdAddr = Input(UInt(10.W))

val rdData = Output(UInt(8.W))

val wrEna = Input(Bool())

val wrData = Input(UInt(8.W))

val wrAddr = Input(UInt(10.W))

})

val mem = SyncReadMem(1024, UInt(8.W))

io.rdData := mem.read(io.rdAddr)

when(io.wrEna) {

mem.write(io.wrAddr, io.wrData)

}

}

31 / 52

Page 32: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Read-During-Write

I What happens when one writes to and reads from thesame address?

I Which value is returned?I Three possibilities:

1. The newly written value2. The old value3. Undefined (mix of old and new)

I Depends on technology, FPGA family, ...I We want to have a defined read-during-writeI We add hardware to forward the written value

32 / 52

Page 33: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Condition for Forwarding

I If read and write addresses are equalI If write enable is trueI Multiplex the output to take the new write value instead of

the (old) read valueI Delay that forwarded write value to have the same timing

33 / 52

Page 34: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Memory with Forwarding

rdAddr

wrAddr

wrData

rdData

wrEna

=AND

dout

Memory

34 / 52

Page 35: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Forwarding in Chisel

val mem = SyncReadMem(1024, UInt(8.W))

val wrDataReg = RegNext(io.wrData)

val doForwardReg = RegNext(io.wrAddr ===

io.rdAddr && io.wrEna)

val memData = mem.read(io.rdAddr)

when(io.wrEna) {

mem.write(io.wrAddr, io.wrData)

}

io.rdData := Mux(doForwardReg , wrDataReg ,

memData)

35 / 52

Page 36: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

External Memory

I On-chip memory is limitedI We can add an external memory chip

I Is cheaper than FPGA on-chip memoryI Sadly the Basys3 board has no external memoryI Simple memory is an asynchronous SRAM

36 / 52

Page 37: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

External SRAM

I We buy a CY7C1041CV33I Let us look into the data sheet

37 / 52

Page 38: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Interfacing the SRAM

I FPGA output drives address, control, and data(sometimes)

I FPGA reads dataI The read signal is asynchronous to the FPGA clockI Do we need an input synchronizer?

38 / 52

Page 39: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Synchronous Interface

I Logic is synchroousI Memory is asynchronous

I How to interface?I Output signals

I Generate timing with synchronous circuitI Small FSM

I Asynchronous input signaleI Usually 2 register for input synchronizationI Really needed for the SRAM interface?I We would loose 2 clock cycles

39 / 52

Page 40: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

SRAM Read

I Asynchronous timing definition (data sheet)I But, we know the timing and we trigger the SRAM address

from our synchronous designI No need to use synchronization registersI Just get the timing correctI Dra the example

I Address - SRAM - dataI Relative to the FPGA clock

40 / 52

Page 41: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Read Timing Continued

I Add all time delaysI Within FPGAI Pad to pinI PSB tracesI SRAM read timingI PCB traces backI Pin to padI Into FPGA register

I Setup and hold time for FPGA registerI Is your today’s lab exercise

41 / 52

Page 42: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Connecting to the World

I Logic in the FPGAI Described in ChiselI Abstracting away electronic properties

I Interface to the worldI Simple switches and LEDsI Did we think about timing?

I FPGA is one component of the systemI Need interconnect to

I Write outputsI Read inputsI Connect to other chips

42 / 52

Page 43: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Bus Interface

I Memory interface can be generalizedI We use a so-called bus to connect several devicesI Usually a microprocessor connected to devices (memory,

IO)I The microprocessor is the masterI A bus is an interface definition

I Logic and timingI Electrical interface

I Parallel or serial dataI Asynchronous or synchronous

I But interface clock is usually not the logic clock

43 / 52

Page 44: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Bus Properties

I Address bus and data busI Control lines (read and write)I Several devices connected

I Multiple outputsI Use tri-state to avoid multiple driver

I Single or multiple masterI Arbitration for multiple master

I Sketch a small microprocessor system

44 / 52

Page 45: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Serial I/O Interface

I Use only one wire for data transferI Bits are serializedI That is where you need your shift register

I Shared wire or dedicated wires for transmit and receiveI Self timed

I Serial UART (RS232)I EthernetI USB

I With a clock signalI SPI, I2C, ...

45 / 52

Page 46: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

RS 232

I Old, but still common interface standardI Was common in 90’ in PCsI Now substituted by USBI Still common in embedded systemsI You Basys3 board has a RS232 interface

I Standard definesI Electrical characteristicsI ’1’ is negative voltage (-15 to -3 V)I ’0’ is positive voltage (+3 to +15 V)I Converted by a RS232 driver to normal logic voltage levels

46 / 52

Page 47: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Serial Transmission

I Transmission consists ofI Start bitI 8 data bitsI Stop bit(s)

I Common baud rate 115200 bits/s

b0 b1 b2 b3 b4 b5 b6 b7

47 / 52

Page 48: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

RS 232 Interface

I Generate bit clock with with counterI Like clock tick generation for display multiplexer

I Output (transmit)I Use shift register for parallel to serial conversionI Small FSM to generate start bit, data bits, and stop bits

I Input (receive)I Detect start with the falling edge of the start bitI Position into middle of start bitI Sample individual bitsI Serial to parallel conversion with a shift register

48 / 52

Page 49: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Chisel Code for RS 232

I More explanation can be found in section 11.2I The code is in the Chisel bookI uart.scalaI Also see example usage in chisel-examples repo

49 / 52

Page 50: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

RS 232 on the Basys3

I Basis3 has an FTDI chip for the USB interfaceI USB interface for FPAG programmingI But also to provide a RS 232 to the FPGAI You can talk with the LaptopI Your VM could write out some textI Use the Chisel code I showed youI Open a terminal to watch (show it)

50 / 52

Page 51: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Today’s Lab

I Paper & pencil exercises on SRAM interfacingI On paper or in a plain text editorI As usual, show and discuss with a TAI Exercise description is in DTU Inside file sharingI sram exercise.pdfI SRAM data sheet: CYCC1041...

51 / 52

Page 52: Interfacing and MemoryOverview I Repeat FSMD (for the vending machine) I Interfaces I Memory (intern and extern) I Serial interface (RS 232) I 2 hours lab SRAM exercise I Exercise

Summary

I Use an FSMD for the vending machine and simpleprocessors

I We need to connect to the worldI FPGA (or any chip) is only part of a systemI Bus interface to external devices (e.g., memory)I Serial interface to connect systems

I E.g., your Basys3 board to the laptop

52 / 52