URL
https://opencores.org/ocsvn/s6soc/s6soc/trunk
Subversion Repositories s6soc
Compare Revisions
- This comparison shows the changes necessary to convert path
/s6soc
- from Rev 1 to Rev 2
- ↔ Reverse comparison
Rev 1 → Rev 2
/trunk/bench/cpp/twoc.h
0,0 → 1,46
//////////////////////////////////////////////////////////////////////////// |
// |
// Filename: twoc.h |
// |
// Project: A Doubletime Pipelined FFT |
// |
// Purpose: Some various two's complement related C++ helper routines. |
// Specifically, these help extract signed numbers from |
// packed bitfields, while guaranteeing that the upper bits |
// are properly sign extended (or not) as desired. |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
#ifndef TWOC_H |
#define TWOC_H |
|
extern long sbits(const long val, const int bits); |
extern unsigned long ubits(const long val, const int bits); |
|
#endif |
|
/trunk/bench/cpp/qspiflashsim.cpp
0,0 → 1,476
/////////////////////////////////////////////////////////////////////////// |
// |
// |
// Filename: spiflashsim.cpp |
// |
// Project: Wishbone Controlled Quad SPI Flash Controller |
// |
// Purpose: This library simulates the operation of a Quad-SPI commanded |
// flash, such as the S25FL032P used on the Basys-3 development |
// board by Digilent. As such, it is defined by 32 Mbits of |
// memory (4 Mbyte). |
// |
// This simulator is useful for testing in a Verilator/C++ |
// environment, where this simulator can be used in place of |
// the actual hardware. |
// |
// Creator: Dan Gisselquist |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
#include <stdio.h> |
#include <string.h> |
#include <assert.h> |
#include <stdlib.h> |
|
#include "qspiflashsim.h" |
|
#define MEMBYTES (1<<22) |
|
static const unsigned DEVID = 0x0115, |
DEVESD = 0x014, |
MICROSECONDS = 100, |
MILLISECONDS = MICROSECONDS * 1000, |
SECONDS = MILLISECONDS * 1000, |
tW = 50 * MICROSECONDS, // write config cycle time |
tBE = 32 * SECONDS, |
tDP = 10 * SECONDS, |
tRES = 30 * SECONDS, |
// Shall we artificially speed up this process? |
tPP = 12 * MICROSECONDS, |
tSE = 15 * MILLISECONDS; |
// or keep it at the original speed |
// tPP = 1200 * MICROSECONDS, |
// tSE = 1500 * MILLISECONDS; |
|
QSPIFLASHSIM::QSPIFLASHSIM(void) { |
m_mem = new char[MEMBYTES]; |
m_pmem = new char[256]; |
m_state = QSPIF_IDLE; |
m_last_sck = 1; |
m_write_count = 0; |
m_ireg = m_oreg = 0; |
m_sreg = 0x01c; |
m_creg = 0x001; // Iinitial creg on delivery |
m_quad_mode = false; |
m_mode_byte = 0; |
|
memset(m_mem, 0x0ff, MEMBYTES); |
} |
|
void QSPIFLASHSIM::load(const unsigned addr, const char *fname) { |
FILE *fp; |
size_t len; |
|
if (addr >= MEMBYTES) |
return; |
len = MEMBYTES-addr*4; |
|
if (NULL != (fp = fopen(fname, "r"))) { |
int nr = 0; |
nr = fread(&m_mem[addr], sizeof(char), len, fp); |
fclose(fp); |
if (nr == 0) { |
fprintf(stderr, "SPI-FLASH: Could not read %s\n", fname); |
perror("O/S Err:"); |
} |
} else { |
fprintf(stderr, "SPI-FLASH: Could not open %s\n", fname); |
perror("O/S Err:"); |
} |
} |
|
#define QOREG(A) m_oreg = ((m_oreg & (~0x0ff))|(A&0x0ff)) |
|
int QSPIFLASHSIM::operator()(const int csn, const int sck, const int dat) { |
// Keep track of a timer to determine when page program and erase |
// cycles complete. |
|
if (m_write_count > 0) { |
if (0 == (--m_write_count)) {// When done with erase/page pgm, |
m_sreg &= 0x0fc; // Clear the write in progress bit |
if (m_debug) printf("Write complete, clearing WIP (inside SIM)\n"); |
} |
} |
|
if (csn) { |
m_last_sck = 1; |
m_ireg = 0; m_oreg = 0; |
m_count= 0; |
|
if ((QSPIF_PP == m_state)||(QSPIF_QPP == m_state)) { |
// Start a page program |
if (m_debug) printf("QSPI: Page Program write cycle begins\n"); |
if (m_debug) printf("CK = %d & 7 = %d\n", m_count, m_count & 0x07); |
if (m_debug) printf("QSPI: pmem = %08lx\n", (unsigned long)m_pmem); |
m_write_count = tPP; |
m_state = QSPIF_IDLE; |
m_sreg &= (~QSPIF_WEL_FLAG); |
m_sreg |= (QSPIF_WIP_FLAG); |
for(int i=0; i<256; i++) { |
/* |
if (m_debug) printf("%02x: m_mem[%02x] = %02x &= %02x = %02x\n", |
i, (m_addr&(~0x0ff))+i, |
m_mem[(m_addr&(~0x0ff))+i]&0x0ff, m_pmem[i]&0x0ff, |
m_mem[(m_addr&(~0x0ff))+i]& m_pmem[i]&0x0ff); |
*/ |
m_mem[(m_addr&(~0x0ff))+i] &= m_pmem[i]; |
} |
m_quad_mode = false; |
} else if (m_state == QSPIF_SECTOR_ERASE) { |
if (m_debug) printf("Actually Erasing sector, from %08x\n", m_addr); |
m_write_count = tSE; |
m_state = QSPIF_IDLE; |
m_sreg &= (~QSPIF_WEL_FLAG); |
m_sreg |= (QSPIF_WIP_FLAG); |
m_addr &= (-1<<16); |
for(int i=0; i<(1<<16); i++) |
m_mem[m_addr + i] = 0x0ff; |
if (m_debug) printf("Now waiting %d ticks delay\n", m_write_count); |
} else if (QSPIF_WRSR == m_state) { |
if (m_debug) printf("Actually writing status register\n"); |
m_write_count = tW; |
m_state = QSPIF_IDLE; |
m_sreg &= (~QSPIF_WEL_FLAG); |
m_sreg |= (QSPIF_WIP_FLAG); |
} else if (QSPIF_CLSR == m_state) { |
if (m_debug) printf("Actually clearing the status register bits\n"); |
m_state = QSPIF_IDLE; |
m_sreg &= 0x09f; |
} else if (m_state == QSPIF_BULK_ERASE) { |
m_write_count = tBE; |
m_state = QSPIF_IDLE; |
m_sreg &= (~QSPIF_WEL_FLAG); |
m_sreg |= (QSPIF_WIP_FLAG); |
for(int i=0; i<MEMBYTES; i++) |
m_mem[i] = 0x0ff; |
} else if (m_state == QSPIF_DEEP_POWER_DOWN) { |
m_write_count = tDP; |
m_state = QSPIF_IDLE; |
} else if (m_state == QSPIF_RELEASE) { |
m_write_count = tRES; |
m_state = QSPIF_IDLE; |
} else if (m_state == QSPIF_QUAD_READ_CMD) { |
if ((m_mode_byte & 0x0f0)!=0x0a0) |
m_quad_mode = false; |
else |
m_state = QSPIF_QUAD_READ_IDLE; |
} else if (m_state == QSPIF_QUAD_READ) { |
if ((m_mode_byte & 0x0f0)!=0x0a0) |
m_quad_mode = false; |
else |
m_state = QSPIF_QUAD_READ_IDLE; |
} else if (m_state == QSPIF_QUAD_READ_IDLE) { |
} |
|
m_oreg = 0x0fe; |
return dat; |
} else if ((!m_last_sck)||(sck == m_last_sck)) { |
// Only change on the falling clock edge |
// printf("SFLASH-SKIP, CLK=%d -> %d\n", m_last_sck, sck); |
m_last_sck = sck; |
if (m_quad_mode) |
return (m_oreg>>8)&0x0f; |
else |
// return ((m_oreg & 0x0100)?2:0) | (dat & 0x0d); |
return (m_oreg & 0x0100)?2:0; |
} |
|
// We'll only get here if ... |
// last_sck = 1, and sck = 0, thus transitioning on the |
// negative edge as with everything else in this interface |
if (m_quad_mode) { |
m_ireg = (m_ireg << 4) | (dat & 0x0f); |
m_count+=4; |
m_oreg <<= 4; |
} else { |
m_ireg = (m_ireg << 1) | (dat & 1); |
m_count++; |
m_oreg <<= 1; |
} |
|
|
// printf("PROCESS, COUNT = %d, IREG = %02x\n", m_count, m_ireg); |
if (m_state == QSPIF_QUAD_READ_IDLE) { |
assert(m_quad_mode); |
if (m_count == 24) { |
if (m_debug) printf("QSPI: Entering from Quad-Read Idle to Quad-Read\n"); |
if (m_debug) printf("QSPI: QI/O Idle Addr = %02x\n", m_ireg&0x0ffffff); |
m_addr = (m_ireg) & 0x0ffffff; |
assert((m_addr & 0xfc00000)==0); |
m_state = QSPIF_QUAD_READ; |
} m_oreg = 0; |
} else if (m_count == 8) { |
QOREG(0x0a5); |
// printf("SFLASH-CMD = %02x\n", m_ireg & 0x0ff); |
// Figure out what command we've been given |
if (m_debug) printf("SPI FLASH CMD %02x\n", m_ireg&0x0ff); |
switch(m_ireg & 0x0ff) { |
case 0x01: // Write status register |
if (2 !=(m_sreg & 0x203)) { |
if (m_debug) printf("QSPI: WEL not set, cannot write status reg\n"); |
m_state = QSPIF_INVALID; |
} else |
m_state = QSPIF_WRSR; |
break; |
case 0x02: // Page program |
if (2 != (m_sreg & 0x203)) { |
if (m_debug) printf("QSPI: Cannot program at this time, SREG = %x\n", m_sreg); |
m_state = QSPIF_INVALID; |
} else { |
m_state = QSPIF_PP; |
if (m_debug) printf("PAGE-PROGRAM COMMAND ACCEPTED\n"); |
} |
break; |
case 0x03: // Read data bytes |
// Our clock won't support this command, so go |
// to an invalid state |
if (m_debug) printf("QSPI INVALID: This sim does not support slow reading\n"); |
m_state = QSPIF_INVALID; |
break; |
case 0x04: // Write disable |
m_state = QSPIF_IDLE; |
m_sreg &= (~QSPIF_WEL_FLAG); |
break; |
case 0x05: // Read status register |
m_state = QSPIF_RDSR; |
if (m_debug) printf("QSPI: READING STATUS REGISTER: %02x\n", m_sreg); |
QOREG(m_sreg); |
break; |
case 0x06: // Write enable |
m_state = QSPIF_IDLE; |
m_sreg |= QSPIF_WEL_FLAG; |
if (m_debug) printf("QSPI: WRITE-ENABLE COMMAND ACCEPTED\n"); |
break; |
case 0x0b: // Here's the read that we support |
if (m_debug) printf("QSPI: FAST-READ (single-bit)\n"); |
m_state = QSPIF_FAST_READ; |
break; |
case 0x30: |
if (m_debug) printf("QSPI: CLEAR STATUS REGISTER COMMAND\n"); |
m_state = QSPIF_CLSR; |
break; |
case 0x32: // QUAD Page program, 4 bits at a time |
if (2 != (m_sreg & 0x203)) { |
if (m_debug) printf("QSPI: Cannot program at this time, SREG = %x\n", m_sreg); |
m_state = QSPIF_INVALID; |
} else { |
m_state = QSPIF_QPP; |
if (m_debug) printf("QSPI: QUAD-PAGE-PROGRAM COMMAND ACCEPTED\n"); |
if (m_debug) printf("QSPI: pmem = %08lx\n", (unsigned long)m_pmem); |
} |
break; |
case 0x35: // Read configuration register |
m_state = QSPIF_RDCR; |
if (m_debug) printf("QSPI: READING CONFIGURATION REGISTER: %02x\n", m_creg); |
QOREG(m_creg); |
break; |
case 0x9f: // Read ID |
m_state = QSPIF_RDID; |
if (m_debug) printf("QSPI: READING ID, %02x\n", (DEVID>>24)&0x0ff); |
QOREG(0xfe); |
break; |
case 0xab: // Release from DEEP POWER DOWN |
if (m_sreg & QSPIF_DEEP_POWER_DOWN_FLAG) { |
if (m_debug) printf("QSPI: Release from deep power down\n"); |
m_sreg &= (~QSPIF_DEEP_POWER_DOWN_FLAG); |
m_write_count = tRES; |
} m_state = QSPIF_RELEASE; |
break; |
case 0xb9: // DEEP POWER DOWN |
if (0 != (m_sreg & 0x01)) { |
if (m_debug) printf("QSPI: Cannot enter DEEP POWER DOWN, in middle of write/erase\n"); |
m_state = QSPIF_INVALID; |
} else { |
m_sreg |= QSPIF_DEEP_POWER_DOWN_FLAG; |
m_state = QSPIF_IDLE; |
} |
break; |
case 0xc7: // Bulk Erase |
if (2 != (m_sreg & 0x203)) { |
if (m_debug) printf("QSPI: WEL not set, cannot erase device\n"); |
m_state = QSPIF_INVALID; |
} else |
m_state = QSPIF_BULK_ERASE; |
break; |
case 0xd8: // Sector Erase |
if (2 != (m_sreg & 0x203)) { |
if (m_debug) printf("QSPI: WEL not set, cannot erase sector\n"); |
m_state = QSPIF_INVALID; |
} else { |
m_state = QSPIF_SECTOR_ERASE; |
if (m_debug) printf("QSPI: SECTOR_ERASE COMMAND\n"); |
} |
break; |
case 0x0eb: // Here's the (other) read that we support |
// printf("QSPI: QUAD-I/O-READ\n"); |
m_state = QSPIF_QUAD_READ_CMD; |
m_quad_mode = true; |
break; |
default: |
printf("QSPI: UNRECOGNIZED SPI FLASH CMD: %02x\n", m_ireg&0x0ff); |
m_state = QSPIF_INVALID; |
assert(0 && "Unrecognized command\n"); |
break; |
} |
} else if ((0 == (m_count&0x07))&&(m_count != 0)) { |
QOREG(0); |
switch(m_state) { |
case QSPIF_IDLE: |
printf("TOO MANY CLOCKS, SPIF in IDLE\n"); |
break; |
case QSPIF_WRSR: |
if (m_count == 16) { |
m_sreg = (m_sreg & 0x061) | (m_ireg & 0x09c); |
if (m_debug) printf("Request to set sreg to 0x%02x\n", |
m_ireg&0x0ff); |
} else if (m_count == 24) { |
m_creg = (m_creg & 0x0fd) | (m_ireg & 0x02); |
if (m_debug) printf("Request to set creg to 0x%02x\n", |
m_ireg&0x0ff); |
} else { |
printf("TOO MANY CLOCKS FOR WRR!!!\n"); |
exit(-2); |
m_state = QSPIF_IDLE; |
} |
break; |
case QSPIF_CLSR: |
assert(0 && "Too many clocks for CLSR command!!\n"); |
break; |
case QSPIF_RDID: |
if (m_count == 32) { |
m_addr = m_ireg & 0x0ffffff; |
if (m_debug) printf("READID, ADDR = %08x\n", m_addr); |
QOREG((DEVID>>8)); |
if (m_debug) printf("QSPI: READING ID, %02x\n", (DEVID>>8)&0x0ff); |
} else if (m_count > 32) { |
if (((m_count-32)>>3)&1) |
QOREG((DEVID)); |
else |
QOREG((DEVID>>8)); |
if (m_debug) printf("QSPI: READING ID, %02x -- DONE\n", 0x00); |
} |
// m_oreg = (DEVID >> (2-(m_count>>3)-1)) & 0x0ff; |
break; |
case QSPIF_RDSR: |
// printf("Read SREG = %02x, wait = %08x\n", m_sreg, |
// m_write_count); |
QOREG(m_sreg); |
break; |
case QSPIF_RDCR: |
if (m_debug) printf("Read CREG = %02x\n", m_creg); |
QOREG(m_creg); |
break; |
case QSPIF_FAST_READ: |
if (m_count == 32) { |
m_addr = m_ireg & 0x0ffffff; |
if (m_debug) printf("FAST READ, ADDR = %08x\n", m_addr); |
QOREG(0x0c3); |
assert((m_addr & 0xfc00000)==0); |
} else if ((m_count >= 40)&&(0 == (m_sreg&0x01))) { |
//if (m_count == 40) |
//printf("DUMMY BYTE COMPLETE ...\n"); |
QOREG(m_mem[m_addr++]); |
// if (m_debug) printf("SPIF[%08x] = %02x\n", m_addr-1, m_oreg); |
} else m_oreg = 0; |
break; |
case QSPIF_QUAD_READ_CMD: |
// The command to go into quad read mode took 8 bits |
// that changes the timings, else we'd use quad_Read |
// below |
if (m_count == 32) { |
m_addr = m_ireg & 0x0ffffff; |
// printf("FAST READ, ADDR = %08x\n", m_addr); |
// printf("QSPI: QUAD READ, ADDR = %06x\n", m_addr); |
assert((m_addr & 0xfc00000)==0); |
} else if (m_count == 32+24) { |
m_mode_byte = (m_ireg>>16) & 0x0ff; |
// printf("QSPI: MODE BYTE = %02x\n", m_mode_byte); |
} else if ((m_count > 32+24)&&(0 == (m_sreg&0x01))) { |
QOREG(m_mem[m_addr++]); |
// printf("QSPIF[%08x]/QR = %02x\n", |
// m_addr-1, m_oreg); |
} else m_oreg = 0; |
break; |
case QSPIF_QUAD_READ: |
if (m_count == 32) { |
m_mode_byte = (m_ireg & 0x0ff); |
// printf("QSPI/QR: MODE BYTE = %02x\n", m_mode_byte); |
} else if ((m_count >= 32+16)&&(0 == (m_sreg&0x01))) { |
QOREG(m_mem[m_addr++]); |
// printf("QSPIF[%08x]/QR = %02x\n", m_addr-1, m_oreg & 0x0ff); |
} else m_oreg = 0; |
break; |
case QSPIF_PP: |
if (m_count == 32) { |
m_addr = m_ireg & 0x0ffffff; |
if (m_debug) printf("QSPI: PAGE-PROGRAM ADDR = %06x\n", m_addr); |
assert((m_addr & 0xfc00000)==0); |
// m_page = m_addr >> 8; |
for(int i=0; i<256; i++) |
m_pmem[i] = 0x0ff; |
} else if (m_count >= 40) { |
m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff; |
// printf("QSPI: PMEM[%02x] = 0x%02x -> %02x\n", m_addr & 0x0ff, m_ireg & 0x0ff, (m_pmem[(m_addr & 0x0ff)]&0x0ff)); |
m_addr = (m_addr & (~0x0ff)) | ((m_addr+1)&0x0ff); |
} break; |
case QSPIF_QPP: |
if (m_count == 32) { |
m_addr = m_ireg & 0x0ffffff; |
m_quad_mode = true; |
if (m_debug) printf("QSPI/QR: PAGE-PROGRAM ADDR = %06x\n", m_addr); |
assert((m_addr & 0xfc00000)==0); |
// m_page = m_addr >> 8; |
for(int i=0; i<256; i++) |
m_pmem[i] = 0x0ff; |
} else if (m_count >= 40) { |
m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff; |
// printf("QSPI/QR: PMEM[%02x] = 0x%02x -> %02x\n", m_addr & 0x0ff, m_ireg & 0x0ff, (m_pmem[(m_addr & 0x0ff)]&0x0ff)); |
m_addr = (m_addr & (~0x0ff)) | ((m_addr+1)&0x0ff); |
} break; |
case QSPIF_SECTOR_ERASE: |
if (m_count == 32) { |
m_addr = m_ireg & 0x0ffc000; |
if (m_debug) printf("SECTOR_ERASE ADDRESS = %08x\n", m_addr); |
assert((m_addr & 0xfc00000)==0); |
} break; |
case QSPIF_RELEASE: |
if (m_count >= 32) { |
QOREG(DEVESD); |
} break; |
default: |
break; |
} |
} // else printf("SFLASH->count = %d\n", m_count); |
|
m_last_sck = sck; |
if (m_quad_mode) |
return (m_oreg>>8)&0x0f; |
else |
// return ((m_oreg & 0x0100)?2:0) | (dat & 0x0d); |
return (m_oreg & 0x0100)?2:0; |
} |
|
/trunk/bench/cpp/uartsim.cpp
0,0 → 1,69
// |
// |
// Filename: uartsim.cpp |
// |
// Project: FPGA library development (S6 development board) |
// |
// Purpose: To emulate the external parameters of a UART device, providing |
// the UART output to, and input from, the command |
// console/terminal while also providing the controller with |
// appropriate busy lines as necessary. |
// |
// Creator: Dan Gisselquist |
// Gisselquist Tecnology, LLC |
// |
// Copyright: 2016 |
// |
// |
#include <unistd.h> |
#include <fcntl.h> |
#include <stdio.h> |
|
#include "uartsim.h" |
|
UARTSIM::UARTSIM(int baud_counts, int fdin, int fdout) { |
int fctl_flags; |
|
m_fdin = fdin; |
m_fdout = fdout; |
m_baud_counts = baud_counts; |
|
m_tx_busy_count = 0; |
m_rx_busy_count = -1; |
|
// Set the linux O_NONBLOCK on fdin |
fctl_flags = fcntl(fdin, F_GETFL, 0); |
fcntl(fdin, F_SETFL, fctl_flags | O_NONBLOCK); |
} |
|
int UARTSIM::rx(unsigned char &data) { |
if (m_rx_busy_count >= 0) { |
if (m_rx_busy_count-- == 0) { |
data = (unsigned char)m_rx_next; |
return 1; |
} |
} |
|
if (read(m_fdin, &m_rx_next, 1) > 0) |
m_rx_busy_count = m_baud_counts; |
|
return 0; |
} |
|
int UARTSIM::tx(int stb, char data) { |
if (m_tx_busy_count > 0) { |
|
// This write may block--if so, we don't care. Just write it. |
if (--m_tx_busy_count == 0) |
if (write(m_fdout, &m_tx_data, 1) != 1) |
perror("O/S Write Err:"); |
return 1; |
} else if (stb) { |
m_tx_data = data; |
m_tx_busy_count = m_baud_counts; |
} |
|
return 0; |
} |
|
|
/trunk/bench/cpp/testb.h
0,0 → 1,70
//////////////////////////////////////////////////////////////////////////////// |
// |
// Filename: testb.h |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU core |
// |
// Purpose: A wrapper for a common interface to a clocked FPGA core |
// begin exercised in Verilator. |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
//////////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
//////////////////////////////////////////////////////////////////////////////// |
#ifndef TESTB_H |
#define TESTB_H |
|
template <class VA> class TESTB { |
public: |
VA *m_core; |
unsigned long m_tickcount; |
|
TESTB(void) { m_core = new VA; } |
virtual ~TESTB(void) { delete m_core; m_core = NULL; } |
|
virtual void eval(void) { |
m_core->eval(); |
} |
|
virtual void tick(void) { |
m_core->i_clk = 1; |
eval(); |
m_core->i_clk = 0; |
eval(); |
|
m_tickcount++; |
} |
|
virtual void reset(void) { |
m_core->i_rst = 1; |
tick(); |
m_core->i_rst = 0; |
m_tickcount = 0l; |
// printf("RESET\n"); |
} |
}; |
|
#endif |
/trunk/bench/cpp/qspiflashsim.h
0,0 → 1,82
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: spiflashsim.h |
// |
// Project: Wishbone Controlled Quad SPI Flash Controller |
// |
// Purpose: This library simulates the operation of a Quad-SPI commanded |
// flash, such as the S25FL032P used on the Basys-3 development |
// board by Digilent. As such, it is defined by 32 Mbits of |
// memory (4 Mbyte). |
// |
// Creator: Dan Gisselquist |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
#ifndef QSPIFLASHSIM_H |
#define QSPIFLASHSIM_H |
|
#define QSPIF_WIP_FLAG 0x0001 |
#define QSPIF_WEL_FLAG 0x0002 |
#define QSPIF_DEEP_POWER_DOWN_FLAG 0x0200 |
class QSPIFLASHSIM { |
typedef enum { |
QSPIF_IDLE, |
QSPIF_QUAD_READ_IDLE, |
QSPIF_RDSR, |
QSPIF_RDCR, |
QSPIF_WRSR, |
QSPIF_CLSR, |
QSPIF_RDID, |
QSPIF_RELEASE, |
QSPIF_FAST_READ, |
QSPIF_QUAD_READ_CMD, |
QSPIF_QUAD_READ, |
QSPIF_SECTOR_ERASE, |
QSPIF_PP, |
QSPIF_QPP, |
QSPIF_BULK_ERASE, |
QSPIF_DEEP_POWER_DOWN, |
QSPIF_INVALID |
} QSPIF_STATE; |
|
QSPIF_STATE m_state; |
char *m_mem, *m_pmem; |
int m_last_sck; |
unsigned m_write_count, m_ireg, m_oreg, m_sreg, m_addr, |
m_count, m_config, m_mode_byte, m_creg; |
bool m_quad_mode, m_debug; |
|
public: |
QSPIFLASHSIM(void); |
void load(const char *fname) { load(0, fname); } |
void load(const unsigned addr, const char *fname); |
void debug(const bool dbg) { m_debug = dbg; } |
bool debug(void) const { return m_debug; } |
int operator()(const int csn, const int sck, const int dat); |
}; |
|
#endif |
/trunk/bench/cpp/uartsim.h
0,0 → 1,33
// |
// |
// Filename: uartsim.h |
// |
// Project: FPGA library development (S6 development board) |
// |
// Purpose: To emulate the external parameters of a UART device, providing |
// the UART output to, and input from, the command |
// console/terminal while also providing the controller with |
// appropriate busy lines as necessary. |
// |
// Creator: Dan Gisselquist |
// Gisselquist Tecnology, LLC |
// |
// Copyright: 2016 |
// |
// |
#include <unistd.h> |
|
class UARTSIM { |
private: |
int m_tx_busy_count, m_baud_counts, m_rx_busy_count; |
int m_fdin, m_fdout; |
char m_rx_next, m_tx_data; |
|
public: |
UARTSIM(int baud_counts, int fdin = STDIN_FILENO, |
int fdout=STDOUT_FILENO); |
int rx(unsigned char &data); |
int tx(int stb, char data); |
}; |
|
|
/trunk/bench/cpp/zip_sim.cpp
0,0 → 1,211
// |
// |
// Filename: busmaster_tb.cpp |
// |
// Project: FPGA library development (S6 development board) |
// |
// Purpose: |
// |
// Creator: Dan Gisselquist |
// Gisselquist Tecnology, LLC |
// |
// Copyright: 2015 |
// |
// |
#include <stdio.h> |
#include <sys/types.h> |
#include <sys/stat.h> |
#include <fcntl.h> |
#include <signal.h> |
#include <time.h> |
#include <unistd.h> |
|
#include "verilated.h" |
#include "Vbusmaster.h" |
|
#include "testb.h" |
// #include "twoc.h" |
#include "qspiflashsim.h" |
#include "uartsim.h" |
|
class GPIOSIM { |
public: |
unsigned operator()(const unsigned o_gpio) { return 0; } |
}; |
|
class KEYPADSIM { |
public: |
unsigned operator()(const unsigned o_kpd) { return 0; } |
}; |
|
// Add a reset line, since Vbusmaster doesn't have one |
class Vbusmasterr : public Vbusmaster { |
public: |
int i_rst; |
}; |
|
// No particular "parameters" need definition or redefinition here. |
class ZIPSIM_TB : public TESTB<Vbusmasterr> { |
public: |
QSPIFLASHSIM m_flash; |
UARTSIM m_uart; |
GPIOSIM m_gpio; |
KEYPADSIM m_keypad; |
unsigned m_last_led; |
time_t m_start_time; |
|
ZIPSIM_TB(void) : m_uart(0x2b6) { |
m_start_time = time(NULL); |
} |
|
void reset(void) { |
m_flash.debug(false); |
} |
|
void tick(void) { |
if ((m_tickcount & ((1<<28)-1))==0) { |
double ticks_per_second = m_tickcount; |
time_t nsecs = (time(NULL)-m_start_time); |
if (nsecs > 0) { |
ticks_per_second /= (double)nsecs; |
printf(" ******** %.6f TICKS PER SECOND\n", |
ticks_per_second); |
} |
} |
|
// Set up the bus before any clock tick |
|
// We've got the flash to deal with ... |
m_core->i_qspi_dat = m_flash(m_core->o_qspi_cs_n, |
m_core->o_qspi_sck, |
m_core->o_qspi_dat); |
|
// And the GPIO lines |
m_core->i_gpio = m_gpio(m_core->o_gpio); |
|
m_core->i_btn = 0; // 2'b0 |
// o_led, o_pwm, o_pwm_aux |
|
// And the keypad |
m_core->i_kp_row = m_keypad(m_core->o_kp_col); |
|
// And the UART |
m_core->i_rx_stb = m_uart.rx(m_core->i_rx_data); |
m_core->i_tx_busy = m_uart.tx(m_core->o_tx_stb, m_core->o_tx_data); |
|
TESTB<Vbusmasterr>::tick(); |
|
if (m_core->o_led != m_last_led) { |
printf("LED: %08x\n", m_core->o_led); |
} |
|
/* |
|
printf("PC: %08x:%08x [%08x:%08x:%08x:%08x:%08x],%08x,%08x,%d,%08x,%08x\n", |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__ipc, |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__upc, |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[0], |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[1], |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[2], |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[3], |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[15], |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_decoder__DOT__r_I, |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__r_opB, |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_decoder__DOT__w_dcdR_pc, |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__r_opA, |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__wr_reg_vl); |
*/ |
|
/* |
if (m_core->v__DOT__thecpu__DOT__thecpu__DOT__pf_valid) |
printf("PC: %08x - %08x, uart=%d,%d, pic = %d,%04x,%0d,%04x\n", |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_pc, |
m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction, |
m_core->i_rx_stb, m_core->i_tx_busy, |
m_core->v__DOT__pic__DOT__r_gie, |
m_core->v__DOT__pic__DOT__r_int_enable, |
m_core->v__DOT__pic__DOT__r_any, |
m_core->v__DOT__pic__DOT__r_int_state); |
*/ |
} |
}; |
|
ZIPSIM_TB *tb; |
|
bool iself(const char *fname) { |
FILE *fp; |
bool ret = true; |
fp = fopen(fname, "rb"); |
|
if (!fp) |
return false; |
if (0x7f != fgetc(fp)) |
ret = false; |
if ('E' != fgetc(fp)) |
ret = false; |
if ('L' != fgetc(fp)) |
ret = false; |
if ('F' != fgetc(fp)) |
ret = false; |
|
fclose(fp); |
return ret; |
} |
|
void usage(void) { |
fprintf(stderr, "Usage: zip_sim flash_program\n"); |
} |
|
int main(int argc, char **argv) { |
Verilated::commandArgs(argc, argv); |
tb = new ZIPSIM_TB; |
const char *codef = NULL; |
|
for(int argn=1; argn<argc; argn++) { |
if (argv[argn][0] == '-') { |
usage(); |
exit(-1); |
} else |
codef = argv[argn]; |
} |
|
if ((!codef)||(!codef[0])) |
fprintf(stderr, "No executable code filename found!\n"); |
|
if (access(codef, R_OK)!=0) |
fprintf(stderr, "Cannot read code filename, %s\n", codef); |
|
if (iself(codef)) { |
char tmpbuf[TMP_MAX], cmdbuf[256]; |
int unused_fd; |
strcpy(tmpbuf, "/var/tmp/zipsimXXXX"); |
|
// Make a temporary file |
unused_fd = mkostemp(tmpbuf, O_CREAT|O_TRUNC|O_RDWR); |
// Close it, though, since we don't want to write to it here |
close(unused_fd); |
|
// Now we right to it, as part of calling objcopy |
sprintf(cmdbuf, "zip-objcopy -S -O binary --reverse-bytes=4 %s %s\n", |
codef, tmpbuf); |
if (system(cmdbuf) != 0) { |
unlink(tmpbuf); |
fprintf(stderr, "ZIP_SIM::Could not convert ELF binary to a raw file\n"); |
exit(-2); |
} |
|
tb->m_flash.load(0x0400, tmpbuf); |
unlink(tmpbuf); |
} else { |
tb->m_flash.load(0x0400, codef); |
} |
|
tb->reset(); |
|
while(1) |
tb->tick(); |
|
printf("SUCCESS!\n"); |
exit(0); |
} |
|
/trunk/bench/cpp/twoc.cpp
0,0 → 1,55
//////////////////////////////////////////////////////////////////////////// |
// |
// Filename: twoc.cpp |
// |
// Project: A Doubletime Pipelined FFT |
// |
// Purpose: Some various two's complement related C++ helper routines. |
// Specifically, these help extract signed numbers from |
// packed bitfields, while guaranteeing that the upper bits |
// are properly sign extended (or not) as desired. |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
#include "twoc.h" |
|
long sbits(const long val, const int bits) { |
long r; |
|
r = val & ((1l<<bits)-1); |
if (r & (1l << (bits-1))) |
r |= (-1l << bits); |
return r; |
} |
|
unsigned long ubits(const long val, const int bits) { |
unsigned long r = val & ((1l<<bits)-1); |
return r; |
} |
|
|
/trunk/bench/cpp/Makefile
0,0 → 1,70
################################################################################ |
# |
# Filename: Makefile |
# |
# Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
# |
# Purpose: This makefile builds the final verilator simulation of the |
# zipsystem. Specifically, it builds the final C++ portion |
# of the simulator, and thus the final simulator executable. |
# |
# |
# Creator: Dan Gisselquist, Ph.D. |
# Gisselquist Technology, LLC |
# |
################################################################################ |
# |
# Copyright (C) 2015, Gisselquist Technology, LLC |
# |
# This program is free software (firmware): 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. |
# |
# This program is distributed in the hope that it will be useful, but WITHOUT |
# ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
# for more details. |
# |
# License: GPL, v3, as defined and found on www.gnu.org, |
# http://www.gnu.org/licenses/gpl.html |
# |
# |
################################################################################ |
# |
all: zip_sim |
|
CXX := g++ |
FLAGS := -Wall -Og -g |
ZASM := ../../sw/zasm |
RTLD := ../../rtl |
INCS := -I$(RTLD)/obj_dir/ -I$(RTLD) -I/usr/share/verilator/include -I$(ZASM) |
SOURCES := zip_sim.cpp twoc.cpp qspiflashsim.cpp uartsim.cpp |
VLIB := /usr/share/verilator/include/verilated.cpp |
RAWLIB := $(VLIB) $(RTLD)/obj_dir/Vbusmaster__ALL.a |
LIBS := $(RAWLIB) |
TESTF := $(ZASM)/z.out |
DHRYSTONEF := ../asm/zipdhry.z |
|
zip_sim: $(SOURCES) $(RAWLIB) testb.h |
$(CXX) $(FLAGS) $(INCS) $(SOURCES) $(LIBS) -o $@ |
|
# .PHONY: stest |
# stest: zippy_tb |
# ./zippy_tb -s $(TESTF) |
|
# .PHONY: itest |
# itest: zippy_tb |
# ./zippy_tb $(TESTF) |
|
# .PHONY: test |
# test: zippy_tb stest |
# ./zippy_tb -a $(TESTF) |
|
# .PHONY: dhrystone |
# dhrystone: zippy_tb |
# ./zippy_tb -a $(DHRYSTONEF) |
|
.PHONY: clean |
clean: |
rm ./zip_sim |
/trunk/rtl/memdev.v
0,0 → 1,21
module memdev(i_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, |
o_wb_ack, o_wb_stall, o_wb_data); |
parameter AW=15, DW=32; |
input i_clk, i_wb_cyc, i_wb_stb, i_wb_we; |
input [(AW-1):0] i_wb_addr; |
input [(DW-1):0] i_wb_data; |
output reg o_wb_ack; |
output wire o_wb_stall; |
output reg [(DW-1):0] o_wb_data; |
|
reg [(DW-1):0] mem [0:((1<<AW)-1)]; |
always @(posedge i_clk) |
o_wb_data <= mem[i_wb_addr]; |
always @(posedge i_clk) |
if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)) |
mem[i_wb_addr] <= i_wb_data; |
always @(posedge i_clk) |
o_wb_ack <= (i_wb_cyc)&&(i_wb_stb); |
assign o_wb_stall = 1'b0; |
|
endmodule |
/trunk/rtl/wbpwmaudio.v
0,0 → 1,191
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: wbpwmaudio.v |
// |
// Project: A Wishbone Controlled PWM (audio) controller |
// |
// Purpose: This PWM controller was designed with audio in mind, although |
// it should be sufficient for many other purposes. Specifically, |
// it creates a pulse-width modulated output, where the amount of time |
// the output is 'high' is determined by the pulse width data given to |
// it. Further, the 'high' time is spread out in bit reversed order. |
// In this fashion, a halfway point will alternate between high and low, |
// rather than the normal fashion of being high for half the time and then |
// low. This approach was chosen to move the PWM artifacts to higher, |
// inaudible frequencies and hence improve the sound quality. |
// |
// The interface supports two addresses: |
// |
// Addr[0] is the data register. Writes to this register will set |
// a 16-bit sample value to be produced by the PWM logic. |
// Reads will also produce, in the 17th bit, whether the interrupt |
// is set or not. (If set, it's time to write a new data value |
// ...) |
// |
// Addr[1] is a timer reload value, used to determine how often the |
// PWM logic needs its next value. This number should be set |
// to the number of clock cycles between reload values. So, |
// for example, an 80 MHz clock can generate a 44.1 kHz audio |
// stream by reading in a new sample every (80e6/44.1e3 = 1814) |
// samples. After loading a sample, the device is immediately |
// ready to load a second. Once the first sample completes, |
// the second sample will start going to the output, and an |
// interrupt will be generated indicating that the device is |
// now ready for the third sample. (The one sample buffer |
// allows some flexibility in getting the new sample there fast |
// enough ...) |
// |
// |
// If you read through the code below, you'll notice that you can also |
// set the timer reload value to an immutable constant by changing the |
// VARIABLE_RATE parameter to 0. When VARIABLE_RATE is set to zero, |
// both addresses become the same, Addr[0] or the data register, and the |
// reload value can no longer be changed--forcing the sample rate to |
// stay constant. |
// |
// |
// Of course, if you don't want to deal with the interrupts or sample |
// rates, you can still get a pseudo analog output by just setting the |
// value to the analog output you would like and then not updating |
// it. In this case, you could also shut the interrupt down at the |
// controller, to keep that from bothering you as well. |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory. Run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
module wbpwmaudio(i_clk, |
// Wishbone interface |
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, |
o_wb_ack, o_wb_stall, o_wb_data, |
o_pwm, o_int); |
parameter DEFAULT_RELOAD = 32'd1814, // about 44.1 kHz @ 80MHz |
//DEFAULT_RELOAD = 32'd2268,//about 44.1 kHz @ 100MHz |
NAUX=2, // Dev control values |
VARIABLE_RATE=0; |
input i_clk; |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input i_wb_addr; |
input [31:0] i_wb_data; |
output reg o_wb_ack; |
output wire o_wb_stall; |
output wire [31:0] o_wb_data; |
output reg o_pwm; |
output reg [(NAUX-1):0] o_aux; |
output reg o_int; |
|
|
// How often shall we create an interrupt? Every reload_value clocks! |
// If VARIABLE_RATE==0, this value will never change and will be kept |
// at the default reload rate (44.1 kHz, for a 100 MHz clock) |
generate |
if (VARIABLE_RATE != 0) |
begin |
reg [31:0] r_reload_value; |
initial r_reload_value = DEFAULT_RELOAD; |
always @(posedge i_clk) // Data write |
if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr)&&(i_wb_we)) |
reload_value <= i_wb_data; |
wire [31:0] w_reload_value; |
assign w_reload_value = r_reload_value; |
end else begin |
wire [31:0] w_reload_value; |
assign w_reload_value = DEFAULT_RELOAD; |
end endgenerate |
|
reg [31:0] reload_value, timer; |
initial reload_value = DEFAULT_RELOAD; |
initial timer = DEFAULT_RELOAD; |
always @(posedge i_clk) |
if (timer == 0) |
timer <= reload_value; |
else |
timer <= timer - 1; |
|
reg [15:0] sample_out; |
always @(posedge i_clk) |
if (timer == 0) |
sample_out <= next_sample; |
|
|
reg [15:0] next_sample; |
reg next_valid; |
initial next_valid = 1'b1; |
initial next_sample = 16'h8000; |
always @(posedge i_clk) // Data write |
if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we) |
&&((~i_wb_addr)||(VARIABLE_RATE==0))) |
begin |
// Write with two's complement data, convert it |
// internally to binary offset |
next_sample <= { ~i_wb_data[15], i_wb_data[14:0] }; |
next_valid <= 1'b1; |
if (i_wb_data[16]) |
o_aux <= i_wb_data[(NAUX+20-1):20]; |
end else if (timer == 0) |
next_valid <= 1'b0; |
|
initial o_int = 1'b0; |
always @(posedge i_clk) |
o_int <= (~next_valid); |
|
reg [15:0] pwm_counter; |
initial pwm_counter = 16'h00; |
always @(posedge i_clk) |
pwm_counter <= pwm_counter + 1; |
|
wire [15:0] br_counter; |
genvar k; |
generate for(k=0; k<16; k=k+1) |
begin : bit_reversal_loop |
assign br_counter[k] = pwm_counter[15-k]; |
end endgenerate |
|
always @(posedge i_clk) |
o_pwm <= (sample_out >= br_counter); |
|
generate |
if (VARIABLE_RATE == 0) |
begin |
assign o_wb_data = { {(12-NAUX){1'b0}}, o_aux, |
3'h0, o_int, sample_out }; |
end else begin |
reg [31:0] r_wb_data; |
always @(posedge i_clk) |
if (i_wb_addr) |
r_wb_data <= reload_value; |
else |
r_wb_data <= { {(12-NAUX){1'b0}}, o_aux, |
3'h0, o_int, sample_out }; |
assign o_wb_data = r_wb_data; |
end endgenerate |
|
initial o_wb_ack = 1'b0; |
always @(posedge i_clk) |
o_wb_ack <= (i_wb_cyc)&&(i_wb_stb); |
assign o_wb_stall = 1'b0; |
|
endmodule |
/trunk/rtl/rtclight.v
0,0 → 1,413
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: rtclight.v |
// |
// Project: A Wishbone Controlled Real--time Clock Core |
// |
// Purpose: Implement a real time clock, including alarm, count--down |
// timer, stopwatch, variable time frequency, and more. |
// |
// This is a light-weight version of the RTC found in this directory. |
// Unlike the full RTC, this version does not support time hacks, seven |
// segment display outputs, or LED's. It is an RTC for an internal core |
// only. (That's how I was using it on one of my projects anyway ...) |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory. Run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
module rtclight(i_clk, |
// Wishbone interface |
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, |
// o_wb_ack, o_wb_stb, o_wb_data, // no reads here |
// // Button inputs |
// i_btn, |
// Output registers |
o_data, // multiplexed based upon i_wb_addr |
// Output controls |
o_interrupt, |
// A once-per-day strobe on the last clock of the day |
o_ppd); |
parameter DEFAULT_SPEED = 32'd2814750; // 100 Mhz |
input i_clk; |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input [2:0] i_wb_addr; |
input [31:0] i_wb_data; |
// input i_btn; |
output reg [31:0] o_data; |
output wire o_interrupt, o_ppd; |
|
reg [21:0] clock; |
reg [31:0] stopwatch, ckspeed; |
reg [25:0] timer; |
|
wire ck_sel, tm_sel, sw_sel, sp_sel, al_sel; |
assign ck_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b000)); |
assign tm_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b001)); |
assign sw_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b010)); |
assign al_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b011)); |
assign sp_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b100)); |
|
reg ck_carry; |
reg [39:0] ck_counter; |
initial ck_carry = 1'b0; |
initial ck_counter = 40'h00; |
always @(posedge i_clk) |
{ ck_carry, ck_counter } <= ck_counter + { 8'h00, ckspeed }; |
|
wire ck_pps; |
reg ck_prepps, ck_ppm, ck_pph, ck_ppd; |
reg [7:0] ck_sub; |
initial clock = 22'h00000; |
assign ck_pps = (ck_carry)&&(ck_prepps); |
always @(posedge i_clk) |
begin |
if (ck_carry) |
ck_sub <= ck_sub + 8'h1; |
ck_prepps <= (ck_sub == 8'hff); |
|
if (ck_pps) |
begin // advance the seconds |
if (clock[3:0] >= 4'h9) |
clock[3:0] <= 4'h0; |
else |
clock[3:0] <= clock[3:0] + 4'h1; |
if (clock[7:0] >= 8'h59) |
clock[7:4] <= 4'h0; |
else if (clock[3:0] >= 4'h9) |
clock[7:4] <= clock[7:4] + 4'h1; |
end |
ck_ppm <= (clock[7:0] == 8'h59); |
|
if ((ck_pps)&&(ck_ppm)) |
begin // advance the minutes |
if (clock[11:8] >= 4'h9) |
clock[11:8] <= 4'h0; |
else |
clock[11:8] <= clock[11:8] + 4'h1; |
if (clock[15:8] >= 8'h59) |
clock[15:12] <= 4'h0; |
else if (clock[11:8] >= 4'h9) |
clock[15:12] <= clock[15:12] + 4'h1; |
end |
ck_pph <= (clock[15:0] == 16'h5959); |
|
if ((ck_pps)&&(ck_pph)) |
begin // advance the hours |
if (clock[21:16] >= 6'h23) |
begin |
clock[19:16] <= 4'h0; |
clock[21:20] <= 2'h0; |
end else if (clock[19:16] >= 4'h9) |
begin |
clock[19:16] <= 4'h0; |
clock[21:20] <= clock[21:20] + 2'h1; |
end else begin |
clock[19:16] <= clock[19:16] + 4'h1; |
end |
end |
ck_ppd <= (clock[21:0] == 22'h235959); |
|
|
if ((ck_sel)&&(i_wb_we)) |
begin |
if (8'hff != i_wb_data[7:0]) |
begin |
clock[7:0] <= i_wb_data[7:0]; |
ck_ppm <= (i_wb_data[7:0] == 8'h59); |
end |
if (8'hff != i_wb_data[15:8]) |
begin |
clock[15:8] <= i_wb_data[15:8]; |
ck_pph <= (i_wb_data[15:8] == 8'h59); |
end |
if (6'h3f != i_wb_data[21:16]) |
clock[21:16] <= i_wb_data[21:16]; |
if (8'h00 == i_wb_data[7:0]) |
ck_sub <= 8'h00; |
end |
end |
|
// Clock updates take several clocks, so let's make sure we |
// are only looking at a valid clock value before testing it. |
reg [21:0] ck_last_clock; |
always @(posedge i_clk) |
ck_last_clock <= clock[21:0]; |
|
|
reg tm_pps, tm_ppm, tm_int; |
wire tm_stopped, tm_running, tm_alarm; |
assign tm_stopped = ~timer[24]; |
assign tm_running = timer[24]; |
assign tm_alarm = timer[25]; |
reg [23:0] tm_start; |
reg [7:0] tm_sub; |
initial tm_start = 24'h00; |
initial timer = 26'h00; |
initial tm_int = 1'b0; |
initial tm_pps = 1'b0; |
always @(posedge i_clk) |
begin |
if (ck_carry) |
begin |
tm_sub <= tm_sub + 8'h1; |
tm_pps <= (tm_sub == 8'hff); |
end else |
tm_pps <= 1'b0; |
|
if ((~tm_alarm)&&(tm_running)&&(tm_pps)) |
begin // If we are running ... |
timer[25] <= 1'b0; |
if (timer[23:0] == 24'h00) |
timer[25] <= 1'b1; |
else if (timer[3:0] != 4'h0) |
timer[3:0] <= timer[3:0]-4'h1; |
else begin // last digit is a zero |
timer[3:0] <= 4'h9; |
if (timer[7:4] != 4'h0) |
timer[7:4] <= timer[7:4]-4'h1; |
else begin // last two digits are zero |
timer[7:4] <= 4'h5; |
if (timer[11:8] != 4'h0) |
timer[11:8] <= timer[11:8]-4'h1; |
else begin // last three digits are zero |
timer[11:8] <= 4'h9; |
if (timer[15:12] != 4'h0) |
timer[15:12] <= timer[15:12]-4'h1; |
else begin |
timer[15:12] <= 4'h5; |
if (timer[19:16] != 4'h0) |
timer[19:16] <= timer[19:16]-4'h1; |
else begin |
// |
timer[19:16] <= 4'h9; |
timer[23:20] <= timer[23:20]-4'h1; |
end |
end |
end |
end |
end |
end |
|
if((~tm_alarm)&&(tm_running)) |
begin |
timer[25] <= (timer[23:0] == 24'h00); |
tm_int <= (timer[23:0] == 24'h00); |
end else tm_int <= 1'b0; |
if (tm_alarm) |
timer[24] <= 1'b0; |
|
if ((tm_sel)&&(i_wb_we)&&(tm_running)) // Writes while running |
// Only allowed to stop the timer, nothing more |
timer[24] <= i_wb_data[24]; |
else if ((tm_sel)&&(i_wb_we)&&(tm_stopped)) // Writes while off |
begin |
timer[24] <= i_wb_data[24]; |
if ((timer[24])||(i_wb_data[24])) |
timer[25] <= 1'b0; |
if (i_wb_data[23:0] != 24'h0000) |
begin |
timer[23:0] <= i_wb_data[23:0]; |
tm_start <= i_wb_data[23:0]; |
tm_sub <= 8'h00; |
end else if (timer[23:0] == 24'h00) |
begin // Resetting timer to last valid timer start val |
timer[23:0] <= tm_start; |
tm_sub <= 8'h00; |
end |
// Any write clears the alarm |
timer[25] <= 1'b0; |
end |
end |
|
// |
// Stopwatch functionality |
// |
// Setting bit '0' starts the stop watch, clearing it stops it. |
// Writing to the register with bit '1' high will clear the stopwatch, |
// and return it to zero provided that the stopwatch is stopped either |
// before or after the write. Hence, writing a '2' to the device |
// will always stop and clear it, whereas writing a '3' to the device |
// will only clear it if it was already stopped. |
reg sw_pps, sw_ppm, sw_pph; |
reg [7:0] sw_sub; |
wire sw_running; |
assign sw_running = stopwatch[0]; |
initial stopwatch = 32'h00000; |
always @(posedge i_clk) |
begin |
sw_pps <= 1'b0; |
if (sw_running) |
begin |
if (ck_carry) |
begin |
sw_sub <= sw_sub + 8'h1; |
sw_pps <= (sw_sub == 8'hff); |
end |
end |
|
stopwatch[7:1] <= sw_sub[7:1]; |
|
if (sw_pps) |
begin // Second hand |
if (stopwatch[11:8] >= 4'h9) |
stopwatch[11:8] <= 4'h0; |
else |
stopwatch[11:8] <= stopwatch[11:8] + 4'h1; |
|
if (stopwatch[15:8] >= 8'h59) |
stopwatch[15:12] <= 4'h0; |
else if (stopwatch[11:8] >= 4'h9) |
stopwatch[15:12] <= stopwatch[15:12] + 4'h1; |
sw_ppm <= (stopwatch[15:8] == 8'h59); |
end else sw_ppm <= 1'b0; |
|
if (sw_ppm) |
begin // Minutes |
if (stopwatch[19:16] >= 4'h9) |
stopwatch[19:16] <= 4'h0; |
else |
stopwatch[19:16] <= stopwatch[19:16]+4'h1; |
|
if (stopwatch[23:16] >= 8'h59) |
stopwatch[23:20] <= 4'h0; |
else if (stopwatch[19:16] >= 4'h9) |
stopwatch[23:20] <= stopwatch[23:20]+4'h1; |
sw_pph <= (stopwatch[23:16] == 8'h59); |
end else sw_pph <= 1'b0; |
|
if (sw_pph) |
begin // And hours |
if (stopwatch[27:24] >= 4'h9) |
stopwatch[27:24] <= 4'h0; |
else |
stopwatch[27:24] <= stopwatch[27:24]+4'h1; |
|
if((stopwatch[27:24] >= 4'h9)&&(stopwatch[31:28] < 4'hf)) |
stopwatch[31:28] <= stopwatch[27:24]+4'h1; |
end |
|
if ((sw_sel)&&(i_wb_we)) |
begin |
stopwatch[0] <= i_wb_data[0]; |
if((i_wb_data[1])&&((~stopwatch[0])||(~i_wb_data[0]))) |
begin |
stopwatch[31:1] <= 31'h00; |
sw_sub <= 8'h00; |
sw_pps <= 1'b0; |
sw_ppm <= 1'b0; |
sw_pph <= 1'b0; |
end |
end |
end |
|
// |
// The alarm code |
// |
// Set the alarm register to the time you wish the board to "alarm". |
// The "alarm" will take place once per day at that time. At that |
// time, the RTC code will generate a clock interrupt, and the CPU/host |
// can come and see that the alarm tripped. |
// |
// |
reg [21:0] alarm_time; |
reg al_int, // The alarm interrupt line |
al_enabled, // Whether the alarm is enabled |
al_tripped; // Whether the alarm has tripped |
initial al_enabled= 1'b0; |
initial al_tripped= 1'b0; |
always @(posedge i_clk) |
begin |
if ((al_sel)&&(i_wb_we)) |
begin |
// Only adjust the alarm hours if the requested hours |
// are valid. This allows writes to the register, |
// without a prior read, to leave these configuration |
// bits alone. |
if (i_wb_data[21:16] != 6'h3f) |
alarm_time[21:16] <= i_wb_data[21:16]; |
// Here's the same thing for the minutes: only adjust |
// the alarm minutes if the new bits are not all 1's. |
if (i_wb_data[15:8] != 8'hff) |
alarm_time[15:8] <= i_wb_data[15:8]; |
// Here's the same thing for the seconds: only adjust |
// the alarm minutes if the new bits are not all 1's. |
if (i_wb_data[7:0] != 8'hff) |
alarm_time[7:0] <= i_wb_data[7:0]; |
al_enabled <= i_wb_data[24]; |
// Reset the alarm if a '1' is written to the tripped |
// register, or if the alarm is disabled. |
if ((i_wb_data[25])||(~i_wb_data[24])) |
al_tripped <= 1'b0; |
end |
|
al_int <= 1'b0; |
if ((ck_last_clock != alarm_time)&&(clock[21:0] == alarm_time) |
&&(al_enabled)) |
begin |
al_tripped <= 1'b1; |
al_int <= 1'b1; |
end |
end |
|
// |
// The ckspeed register is equal to 2^48 divded by the number of |
// clock ticks you expect per second. Adjust high for a slower |
// clock, lower for a faster clock. In this fashion, a single |
// real time clock RTL file can handle tracking the clock in any |
// device. Further, because this is only the lower 32 bits of a |
// 48 bit counter per seconds, the clock jitter is kept below |
// 1 part in 65 thousand. |
// |
initial ckspeed = DEFAULT_SPEED; // 2af31e = 2^48 / 100e6 MHz |
// In the case of verilator, comment the above and uncomment the line |
// below. The clock constant below is "close" to simulation time, |
// meaning that my verilator simulation is running about 300x slower |
// than board time. |
// initial ckspeed = 32'd786432000; |
always @(posedge i_clk) |
if ((sp_sel)&&(i_wb_we)) |
ckspeed <= i_wb_data; |
|
assign o_interrupt = tm_int || al_int; |
|
// A once-per day strobe, on the last second of the day so that the |
// the next clock is the first clock of the day. This is useful for |
// connecting this module to a year/month/date date/calendar module. |
assign o_ppd = (ck_ppd)&&(ck_pps); |
|
always @(posedge i_clk) |
case(i_wb_addr[2:0]) |
3'b000: o_data <= { 10'h0, ck_last_clock }; |
3'b001: o_data <= { 6'h00, timer }; |
3'b010: o_data <= stopwatch; |
3'b011: o_data <= { 6'h00, al_tripped, al_enabled, 2'b00, alarm_time }; |
3'b100: o_data <= ckspeed; |
default: o_data <= 32'h000; |
endcase |
|
endmodule |
/trunk/rtl/flash_config.v
0,0 → 1,48
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: flashconfig.v |
// |
// Project: Wishbone Controlled Quad SPI Flash Controller |
// |
// Purpose: A configuration file, separated from the controller file, so |
// that multiple files can use the same wishbone Quad Spi Flash |
// controller, while each having a separate configuration. Currently, |
// the configuration only includes whether the flash is read only or not. |
// Other configuration options may be added later. |
// |
// |
// Creator: Dan Gisselquist |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
`ifndef FLASH_CONFIG_V |
`define FLASH_CONFIG_V |
// |
`define READ_ONLY |
// |
`endif |
// |
/trunk/rtl/cpu/wbpriarbiter.v
0,0 → 1,117
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: wbpriarbiter.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: This is a priority bus arbiter. It allows two separate wishbone |
// masters to connect to the same bus, while also guaranteeing |
// that one master can have the bus with no delay any time the |
// other master is not using the bus. The goal is to eliminate |
// the combinatorial logic required in the other wishbone |
// arbiter, while still guarateeing access time for the priority |
// channel. |
// |
// The core logic works like this: |
// |
// 1. When no one requests the bus, 'A' is granted the bus and |
// guaranteed that any access will go right through. |
// 2. If 'B' requests the bus (asserts cyc), and the bus is idle, |
// then 'B' will be granted the bus. |
// 3. Bus grants last as long as the 'cyc' line is high. |
// 4. Once 'cyc' is dropped, the bus returns to 'A' as the owner. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
module wbpriarbiter(i_clk, |
// Bus A |
i_a_cyc, i_a_stb, i_a_we, i_a_adr, i_a_dat, o_a_ack, o_a_stall, o_a_err, |
// Bus B |
i_b_cyc, i_b_stb, i_b_we, i_b_adr, i_b_dat, o_b_ack, o_b_stall, o_b_err, |
// Both buses |
o_cyc, o_stb, o_we, o_adr, o_dat, i_ack, i_stall, i_err); |
parameter DW=32, AW=32; |
// |
input i_clk; |
// Bus A |
input i_a_cyc, i_a_stb, i_a_we; |
input [(AW-1):0] i_a_adr; |
input [(DW-1):0] i_a_dat; |
output wire o_a_ack, o_a_stall, o_a_err; |
// Bus B |
input i_b_cyc, i_b_stb, i_b_we; |
input [(AW-1):0] i_b_adr; |
input [(DW-1):0] i_b_dat; |
output wire o_b_ack, o_b_stall, o_b_err; |
// |
output wire o_cyc, o_stb, o_we; |
output wire [(AW-1):0] o_adr; |
output wire [(DW-1):0] o_dat; |
input i_ack, i_stall, i_err; |
|
// Go high immediately (new cycle) if ... |
// Previous cycle was low and *someone* is requesting a bus cycle |
// Go low immadiately if ... |
// We were just high and the owner no longer wants the bus |
// WISHBONE Spec recommends no logic between a FF and the o_cyc |
// This violates that spec. (Rec 3.15, p35) |
assign o_cyc = (r_a_owner) ? i_a_cyc : i_b_cyc; |
reg r_a_owner; |
initial r_a_owner = 1'b1; |
always @(posedge i_clk) |
if (~i_b_cyc) |
r_a_owner <= 1'b1; |
else if ((i_b_cyc)&&(~i_a_cyc)) |
r_a_owner <= 1'b0; |
|
|
// Realistically, if neither master owns the bus, the output is a |
// don't care. Thus we trigger off whether or not 'A' owns the bus. |
// If 'B' owns it all we care is that 'A' does not. Likewise, if |
// neither owns the bus than the values on the various lines are |
// irrelevant. |
assign o_stb = (r_a_owner) ? i_a_stb : i_b_stb; |
assign o_we = (r_a_owner) ? i_a_we : i_b_we; |
assign o_adr = (r_a_owner) ? i_a_adr : i_b_adr; |
assign o_dat = (r_a_owner) ? i_a_dat : i_b_dat; |
|
// We cannot allow the return acknowledgement to ever go high if |
// the master in question does not own the bus. Hence we force it |
// low if the particular master doesn't own the bus. |
assign o_a_ack = ( r_a_owner) ? i_ack : 1'b0; |
assign o_b_ack = (~r_a_owner) ? i_ack : 1'b0; |
|
// Stall must be asserted on the same cycle the input master asserts |
// the bus, if the bus isn't granted to him. |
assign o_a_stall = ( r_a_owner) ? i_stall : 1'b1; |
assign o_b_stall = (~r_a_owner) ? i_stall : 1'b1; |
|
// |
// |
assign o_a_err = ( r_a_owner) ? i_err : 1'b0; |
assign o_b_err = (~r_a_owner) ? i_err : 1'b0; |
|
endmodule |
|
/trunk/rtl/cpu/idecode.v
0,0 → 1,436
/////////////////////////////////////////////////////////////////////////////// |
// |
// Filename: idecode.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: This RTL file specifies how instructions are to be decoded |
// into their underlying meanings. This is specifically a version |
// designed to support a "Next Generation", or "Version 2" instruction |
// set as (currently) activated by the OPT_NEW_INSTRUCTION_SET option |
// in cpudefs.v. |
// |
// I expect to (eventually) retire the old instruction set, at which point |
// this will become the default instruction set decoder. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////////// |
// |
// |
// |
`define CPU_CC_REG 4'he |
`define CPU_PC_REG 4'hf |
// |
`include "cpudefs.v" |
// |
// |
// |
module idecode(i_clk, i_rst, i_ce, i_stalled, |
i_instruction, i_gie, i_pc, i_pf_valid, |
i_illegal, |
o_phase, o_illegal, |
o_pc, o_gie, |
o_dcdR, o_dcdA, o_dcdB, o_I, o_zI, |
o_cond, o_wF, |
o_op, o_ALU, o_M, o_DV, o_FP, o_break, o_lock, |
o_wR, o_rA, o_rB, |
o_early_branch, o_branch_pc, o_ljmp, |
o_pipe |
); |
parameter ADDRESS_WIDTH=24, IMPLEMENT_MPY=1, EARLY_BRANCHING=1, |
IMPLEMENT_DIVIDE=1, IMPLEMENT_FPU=0, AW = ADDRESS_WIDTH; |
input i_clk, i_rst, i_ce, i_stalled; |
input [31:0] i_instruction; |
input i_gie; |
input [(AW-1):0] i_pc; |
input i_pf_valid, i_illegal; |
output wire o_phase; |
output reg o_illegal; |
output reg [(AW-1):0] o_pc; |
output reg o_gie; |
output reg [6:0] o_dcdR, o_dcdA, o_dcdB; |
output wire [31:0] o_I; |
output reg o_zI; |
output reg [3:0] o_cond; |
output reg o_wF; |
output reg [3:0] o_op; |
output reg o_ALU, o_M, o_DV, o_FP, o_break, o_lock; |
output reg o_wR, o_rA, o_rB; |
output wire o_early_branch; |
output wire [(AW-1):0] o_branch_pc; |
output wire o_ljmp; |
output reg o_pipe; |
|
wire dcdA_stall, dcdB_stall, dcdF_stall; |
wire o_dcd_early_branch; |
wire [(AW-1):0] o_dcd_branch_pc; |
reg o_dcdI, o_dcdIz; |
|
|
wire [4:0] w_op; |
wire w_ldi, w_mov, w_cmptst, w_ldixx, w_ALU; |
wire [4:0] w_dcdR, w_dcdB, w_dcdA; |
wire w_dcdR_pc, w_dcdR_cc; |
wire w_dcdA_pc, w_dcdA_cc; |
wire w_dcdB_pc, w_dcdB_cc; |
wire [3:0] w_cond; |
wire w_wF, w_dcdM, w_dcdDV, w_dcdFP; |
wire w_wR, w_rA, w_rB, w_wR_n; |
wire w_ljmp; |
|
generate |
if (EARLY_BRANCHING != 0) |
assign w_ljmp = (iword == 32'h7c87c000); |
else |
assign w_ljmp = 1'b0; |
endgenerate |
|
|
wire [31:0] iword; |
`ifdef OPT_VLIW |
reg [16:0] r_nxt_half; |
assign iword = (o_phase) |
// set second half as a NOOP ... but really |
// shouldn't matter |
? { r_nxt_half[16:7], 1'b0, r_nxt_half[6:0], 5'b11000, 3'h7, 6'h00 } |
: i_instruction; |
`else |
assign iword = { 1'b0, i_instruction[30:0] }; |
`endif |
|
assign w_op= iword[26:22]; |
assign w_mov = (w_op == 5'h0f); |
assign w_ldi = (w_op[4:1] == 4'hb); |
assign w_cmptst = (w_op[4:1] == 4'h8); |
assign w_ldixx = (w_op[4:1] == 4'h4); |
assign w_ALU = (~w_op[4]); |
|
// 4 LUTs |
assign w_dcdR = { ((~iword[31])&&(w_mov)&&(~i_gie))?iword[18]:i_gie, |
iword[30:27] }; |
// 4 LUTs |
assign w_dcdB = { ((~iword[31])&&(w_mov)&&(~i_gie))?iword[13]:i_gie, |
iword[17:14] }; |
|
// 0 LUTs |
assign w_dcdA = w_dcdR; |
// 2 LUTs, 1 delay each |
assign w_dcdR_pc = (w_dcdR == {i_gie, `CPU_PC_REG}); |
assign w_dcdR_cc = (w_dcdR == {i_gie, `CPU_CC_REG}); |
// 0 LUTs |
assign w_dcdA_pc = w_dcdR_pc; |
assign w_dcdA_cc = w_dcdR_cc; |
// 2 LUTs, 1 delays each |
assign w_dcdB_pc = (w_dcdB[3:0] == `CPU_PC_REG); |
assign w_dcdB_cc = (w_dcdB[3:0] == `CPU_CC_REG); |
|
// Under what condition will we execute this |
// instruction? Only the load immediate instruction |
// is completely unconditional. |
// |
// 3+4 LUTs |
assign w_cond = (w_ldi) ? 4'h8 : |
(iword[31])?{(iword[20:19]==2'b00), |
1'b0,iword[20:19]} |
: { (iword[21:19]==3'h0), iword[21:19] }; |
|
// 1 LUT |
assign w_dcdM = (w_op[4:1] == 4'h9); |
// 1 LUT |
assign w_dcdDV = (w_op[4:1] == 4'ha); |
// 1 LUT |
assign w_dcdFP = (w_op[4:3] == 2'b11)&&(w_dcdR[3:1] != 3'h7); |
// 4 LUT's--since it depends upon FP/NOOP condition (vs 1 before) |
// Everything reads A but ... NOOP/BREAK/LOCK, LDI, LOD, MOV |
assign w_rA = (w_dcdFP) |
// Divide's read A |
||(w_dcdDV) |
// ALU read's A, unless it's a MOV to A |
// This includes LDIHI/LDILO |
||((~w_op[4])&&(w_op[3:0]!=4'hf)) |
// STO's read A |
||((w_dcdM)&&(w_op[0])) |
// Test/compares |
||(w_op[4:1]== 4'h8); |
// 1 LUTs -- do we read a register for operand B? Specifically, do |
// we need to stall if the register is not (yet) ready? |
assign w_rB = (w_mov)||((iword[18])&&((~w_ldi)&&(~w_ldixx))); |
// 1 LUT: All but STO, NOOP/BREAK/LOCK, and CMP/TST write back to w_dcdR |
assign w_wR_n = ((w_dcdM)&&(w_op[0])) |
||((w_op[4:3]==2'b11)&&(w_dcdR[3:1]==3'h7)) |
||(w_cmptst); |
assign w_wR = ~w_wR_n; |
// |
// 1-output bit (5 Opcode bits, 4 out-reg bits, 3 condition bits) |
// |
// This'd be 4 LUTs, save that we have the carve out for NOOPs |
// and writes to the PC/CC register(s). |
assign w_wF = (w_cmptst) |
||((w_cond[3])&&((w_dcdFP)||(w_dcdDV) |
||((w_ALU)&&(~w_mov)&&(~w_ldixx) |
&&(iword[30:28] != 3'h7)))); |
|
// Bottom 13 bits: no LUT's |
// w_dcd[12: 0] -- no LUTs |
// w_dcd[ 13] -- 2 LUTs |
// w_dcd[17:14] -- (5+i0+i1) = 3 LUTs, 1 delay |
// w_dcd[22:18] : 5 LUTs, 1 delay (assuming high bit is o/w determined) |
reg [22:0] r_I; |
wire [22:0] w_I, w_fullI; |
wire w_Iz; |
|
assign w_fullI = (w_ldi) ? { iword[22:0] } // LDI |
:((w_mov) ?{ {(23-13){iword[12]}}, iword[12:0] } // Move |
:((~iword[18]) ? { {(23-18){iword[17]}}, iword[17:0] } |
: { {(23-14){iword[13]}}, iword[13:0] } |
)); |
|
`ifdef OPT_VLIW |
wire [5:0] w_halfI; |
assign w_halfI = (w_ldi) ? iword[5:0] |
:((iword[5]) ? 6'h00 : {iword[4],iword[4:0]}); |
assign w_I = (iword[31])? {{(23-6){w_halfI[5]}}, w_halfI }:w_fullI; |
`else |
assign w_I = w_fullI; |
`endif |
assign w_Iz = (w_I == 0); |
|
|
`ifdef OPT_VLIW |
// |
// The o_phase parameter is special. It needs to let the software |
// following know that it cannot break/interrupt on an o_phase asserted |
// instruction, lest the break take place between the first and second |
// half of a VLIW instruction. To do this, o_phase must be asserted |
// when the first instruction half is valid, but not asserted on either |
// a 32-bit instruction or the second half of a 2x16-bit instruction. |
reg r_phase; |
initial r_phase = 1'b0; |
always @(posedge i_clk) |
if (i_rst) // When no instruction is in the pipe, phase is zero |
r_phase <= 1'b0; |
else if (i_ce) |
r_phase <= (o_phase)? 1'b0:(i_instruction[31]); |
// Phase is '1' on the first instruction of a two-part set |
// But, due to the delay in processing, it's '1' when our output is |
// valid for that first part, but that'll be the same time we |
// are processing the second part ... so it may look to us like a '1' |
// on the second half of processing. |
|
assign o_phase = r_phase; |
`else |
assign o_phase = 1'b0; |
`endif |
|
|
initial o_illegal = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
o_illegal <= 1'b0; |
else if (i_ce) |
begin |
`ifdef OPT_VLIW |
o_illegal <= (i_illegal); |
`else |
o_illegal <= ((i_illegal) || (i_instruction[31])); |
`endif |
if ((IMPLEMENT_MPY!=1)&&(w_op[4:1]==4'h5)) |
o_illegal <= 1'b1; |
|
if ((IMPLEMENT_DIVIDE==0)&&(w_dcdDV)) |
o_illegal <= 1'b1; |
else if ((IMPLEMENT_DIVIDE!=0)&&(w_dcdDV)&&(w_dcdR[3:1]==3'h7)) |
o_illegal <= 1'b1; |
|
|
if ((IMPLEMENT_FPU!=0)&&(w_dcdFP)&&(w_dcdR[3:1]==3'h7)) |
o_illegal <= 1'b1; |
else if ((IMPLEMENT_FPU==0)&&(w_dcdFP)) |
o_illegal <= 1'b1; |
|
if ((w_op[4:3]==2'b11)&&(w_dcdR[3:1]==3'h7) |
&&( |
(w_op[2:0] != 3'h2) // LOCK |
&&(w_op[2:0] != 3'h1) // BREAK |
&&(w_op[2:0] != 3'h0))) // NOOP |
o_illegal <= 1'b1; |
end |
|
|
always @(posedge i_clk) |
if (i_ce) |
begin |
`ifdef OPT_VLIW |
if (~o_phase) |
begin |
o_gie<= i_gie; |
// i.e. dcd_pc+1 |
o_pc <= i_pc+{{(AW-1){1'b0}},1'b1}; |
end |
`else |
o_gie<= i_gie; |
o_pc <= i_pc+{{(AW-1){1'b0}},1'b1}; |
`endif |
|
// Under what condition will we execute this |
// instruction? Only the load immediate instruction |
// is completely unconditional. |
o_cond <= w_cond; |
// Don't change the flags on conditional instructions, |
// UNLESS: the conditional instruction was a CMP |
// or TST instruction. |
o_wF <= w_wF; |
|
// Record what operation/op-code (4-bits) we are doing |
// Note that LDI magically becomes a MOV |
// instruction here. That way it's a pass through |
// the ALU. Likewise, the two compare instructions |
// CMP and TST becomes SUB and AND here as well. |
// We keep only the bottom four bits, since we've |
// already done the rest of the decode necessary to |
// settle between the other instructions. For example, |
// o_FP plus these four bits uniquely defines the FP |
// instruction, o_DV plus the bottom of these defines |
// the divide, etc. |
o_op <= (w_ldi)? 4'hf:w_op[3:0]; |
|
// Default values |
o_dcdR <= { w_dcdR_cc, w_dcdR_pc, w_dcdR}; |
o_dcdA <= { w_dcdA_cc, w_dcdA_pc, w_dcdA}; |
o_dcdB <= { w_dcdB_cc, w_dcdB_pc, w_dcdB}; |
o_wR <= w_wR; |
o_rA <= w_rA; |
o_rB <= w_rB; |
r_I <= w_I; |
o_zI <= w_Iz; |
|
o_ALU <= (w_ALU)||(w_ldi)||(w_cmptst); // 1 LUT |
o_M <= w_dcdM; |
o_DV <= w_dcdDV; |
o_FP <= w_dcdFP; |
|
o_break <= (w_op[4:3]==2'b11)&&(w_dcdR[3:1]==3'h7)&&(w_op[2:0]==3'b001); |
o_lock <= (w_op[4:3]==2'b11)&&(w_dcdR[3:1]==3'h7)&&(w_op[2:0]==3'b010); |
`ifdef OPT_VLIW |
r_nxt_half <= { iword[31], iword[13:5], |
((iword[21])? iword[20:19] : 2'h0), |
iword[4:0] }; |
`endif |
end |
|
generate |
if (EARLY_BRANCHING!=0) |
begin |
reg r_early_branch, r_ljmp; |
reg [(AW-1):0] r_branch_pc; |
|
initial r_ljmp = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_ljmp <= 1'b0; |
else if ((i_ce)&&(i_pf_valid)) |
r_ljmp <= (w_ljmp); |
assign o_ljmp = r_ljmp; |
|
always @(posedge i_clk) |
if (i_rst) |
r_early_branch <= 1'b0; |
else if ((i_ce)&&(i_pf_valid)) |
begin |
if (r_ljmp) |
// LOD (PC),PC |
r_early_branch <= 1'b1; |
else if ((~iword[31])&&(iword[30:27]==`CPU_PC_REG)&&(w_cond[3])) |
begin |
if (w_op[4:1] == 4'hb) // LDI to PC |
// LDI x,PC |
r_early_branch <= 1'b1; |
else if ((w_op[4:0]==5'h02)&&(~iword[18])) |
// Add x,PC |
r_early_branch <= 1'b1; |
else begin |
r_early_branch <= 1'b0; |
end |
end else |
r_early_branch <= 1'b0; |
end else if (i_ce) |
r_early_branch <= 1'b0; |
|
always @(posedge i_clk) |
if (i_ce) |
begin |
if (r_ljmp) |
r_branch_pc <= iword[(AW-1):0]; |
else if (w_op[4:1] == 4'hb) // LDI |
r_branch_pc <= {{(AW-23){iword[22]}},iword[22:0]}; |
else // Add x,PC |
r_branch_pc <= i_pc |
+ {{(AW-17){iword[17]}},iword[16:0]} |
+ {{(AW-1){1'b0}},1'b1}; |
end |
|
assign o_early_branch = r_early_branch; |
assign o_branch_pc = r_branch_pc; |
end else begin |
assign o_early_branch = 1'b0; |
assign o_branch_pc = {(AW){1'b0}}; |
assign o_ljmp = 1'b0; |
end endgenerate |
|
|
// To be a pipeable operation there must be ... |
// 1. Two valid adjacent instructions |
// 2. Both must be memory operations, of the same time (both lods |
// or both stos) |
// 3. Both must use the same register base address |
// 4. Both must be to the same address, or the address incremented |
// by one |
// Note that we're not using iword here ... there's a lot of logic |
// taking place, and it's only valid if the new word is not compressed. |
// |
reg r_valid; |
always @(posedge i_clk) |
if (i_ce) |
o_pipe <= (r_valid)&&(i_pf_valid)&&(~i_instruction[31]) |
&&(w_dcdM)&&(o_M)&&(o_op[0] ==i_instruction[22]) |
&&(i_instruction[17:14] == o_dcdB[3:0]) |
&&(i_gie == o_gie) |
&&((i_instruction[21:19]==o_cond[2:0]) |
||(o_cond[2:0] == 3'h0)) |
&&((i_instruction[13:0]==r_I[13:0]) |
||({1'b0, i_instruction[13:0]}==(r_I[13:0]+14'h1))); |
always @(posedge i_clk) |
if (i_rst) |
r_valid <= 1'b0; |
else if ((i_ce)&&(o_ljmp)) |
r_valid <= 1'b0; |
else if ((i_ce)&&(i_pf_valid)) |
r_valid <= 1'b1; |
else if (~i_stalled) |
r_valid <= 1'b0; |
|
|
assign o_I = { {(32-22){r_I[22]}}, r_I[21:0] }; |
|
endmodule |
/trunk/rtl/cpu/prefetch.v
0,0 → 1,124
//////////////////////////////////////////////////////////////////////////////// |
// |
// Filename: prefetch.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: This is a very simple instruction fetch approach. It gets |
// one instruction at a time. Future versions should pipeline |
// fetches and perhaps even cache results--this doesn't do that. |
// It should, however, be simple enough to get things running. |
// |
// The interface is fascinating. The 'i_pc' input wire is just |
// a suggestion of what to load. Other wires may be loaded |
// instead. i_pc is what must be output, not necessarily input. |
// |
// 20150919 -- Added support for the WB error signal. When reading an |
// instruction results in this signal being raised, the pipefetch |
// module will set an illegal instruction flag to be returned to |
// the CPU together with the instruction. Hence, the ZipCPU |
// can trap on it if necessary. |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
//////////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
//////////////////////////////////////////////////////////////////////////////// |
// |
// Flash requires a minimum of 4 clocks per byte to read, so that would be |
// 4*(4bytes/32bit word) = 16 clocks per word read---and that's in pipeline |
// mode which this prefetch does not support. In non--pipelined mode, the |
// flash will require (16+6+6)*2 = 56 clocks plus 16 clocks per word read, |
// or 72 clocks to fetch one instruction. |
module prefetch(i_clk, i_rst, i_ce, i_stalled_n, i_pc, i_aux, |
o_i, o_pc, o_aux, o_valid, o_illegal, |
o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, |
i_wb_ack, i_wb_stall, i_wb_err, i_wb_data); |
parameter ADDRESS_WIDTH=32, AUX_WIDTH = 1, AW=ADDRESS_WIDTH; |
input i_clk, i_rst, i_ce, i_stalled_n; |
input [(AW-1):0] i_pc; |
input [(AUX_WIDTH-1):0] i_aux; |
output reg [31:0] o_i; |
output reg [(AW-1):0] o_pc; |
output reg [(AUX_WIDTH-1):0] o_aux; |
output reg o_valid, o_illegal; |
// Wishbone outputs |
output reg o_wb_cyc, o_wb_stb; |
output wire o_wb_we; |
output reg [(AW-1):0] o_wb_addr; |
output wire [31:0] o_wb_data; |
// And return inputs |
input i_wb_ack, i_wb_stall, i_wb_err; |
input [31:0] i_wb_data; |
|
assign o_wb_we = 1'b0; |
assign o_wb_data = 32'h0000; |
|
// Let's build it simple and upgrade later: For each instruction |
// we do one bus cycle to get the instruction. Later we should |
// pipeline this, but for now let's just do one at a time. |
initial o_wb_cyc = 1'b0; |
initial o_wb_stb = 1'b0; |
initial o_wb_addr= 0; |
always @(posedge i_clk) |
if ((i_rst)||(i_wb_ack)) |
begin |
o_wb_cyc <= 1'b0; |
o_wb_stb <= 1'b0; |
end else if ((i_ce)&&(~o_wb_cyc)) // Initiate a bus cycle |
begin |
o_wb_cyc <= 1'b1; |
o_wb_stb <= 1'b1; |
end else if (o_wb_cyc) // Independent of ce |
begin |
if ((o_wb_cyc)&&(o_wb_stb)&&(~i_wb_stall)) |
o_wb_stb <= 1'b0; |
if (i_wb_ack) |
o_wb_cyc <= 1'b0; |
end |
|
always @(posedge i_clk) |
if (i_rst) // Set the address to guarantee the result is invalid |
o_wb_addr <= {(AW){1'b1}}; |
else if ((i_ce)&&(~o_wb_cyc)) |
o_wb_addr <= i_pc; |
always @(posedge i_clk) |
if ((o_wb_cyc)&&(i_wb_ack)) |
o_aux <= i_aux; |
always @(posedge i_clk) |
if ((o_wb_cyc)&&(i_wb_ack)) |
o_i <= i_wb_data; |
always @(posedge i_clk) |
if ((o_wb_cyc)&&(i_wb_ack)) |
o_pc <= o_wb_addr; |
initial o_valid = 1'b0; |
initial o_illegal = 1'b0; |
always @(posedge i_clk) |
if ((o_wb_cyc)&&(i_wb_ack)) |
begin |
o_valid <= (i_pc == o_wb_addr)&&(~i_wb_err); |
o_illegal <= i_wb_err; |
end else if (i_stalled_n) |
begin |
o_valid <= 1'b0; |
o_illegal <= 1'b0; |
end |
|
endmodule |
/trunk/rtl/cpu/memops.v
0,0 → 1,150
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: memops.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: A memory unit to support a CPU. |
// |
// In the interests of code simplicity, this memory operator is |
// susceptible to unknown results should a new command be sent to it |
// before it completes the last one. Unpredictable results might then |
// occurr. |
// |
// 20150919 -- Added support for handling BUS ERR's (i.e., the WB |
// error signal). |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
module memops(i_clk, i_rst, i_stb, i_lock, |
i_op, i_addr, i_data, i_oreg, |
o_busy, o_valid, o_err, o_wreg, o_result, |
o_wb_cyc_gbl, o_wb_cyc_lcl, |
o_wb_stb_gbl, o_wb_stb_lcl, |
o_wb_we, o_wb_addr, o_wb_data, |
i_wb_ack, i_wb_stall, i_wb_err, i_wb_data); |
parameter ADDRESS_WIDTH=24, IMPLEMENT_LOCK=0, AW=ADDRESS_WIDTH; |
input i_clk, i_rst; |
input i_stb, i_lock; |
// CPU interface |
input i_op; |
input [31:0] i_addr; |
input [31:0] i_data; |
input [4:0] i_oreg; |
// CPU outputs |
output wire o_busy; |
output reg o_valid; |
output reg o_err; |
output reg [4:0] o_wreg; |
output reg [31:0] o_result; |
// Wishbone outputs |
output wire o_wb_cyc_gbl; |
output reg o_wb_stb_gbl; |
output wire o_wb_cyc_lcl; |
output reg o_wb_stb_lcl; |
output reg o_wb_we; |
output reg [(AW-1):0] o_wb_addr; |
output reg [31:0] o_wb_data; |
// Wishbone inputs |
input i_wb_ack, i_wb_stall, i_wb_err; |
input [31:0] i_wb_data; |
|
reg r_wb_cyc_gbl, r_wb_cyc_lcl; |
wire gbl_stb, lcl_stb; |
assign lcl_stb = (i_stb)&&(i_addr[31:8]==24'hc00000)&&(i_addr[7:5]==3'h0); |
assign gbl_stb = (i_stb)&&((i_addr[31:8]!=24'hc00000)||(i_addr[7:5]!=3'h0)); |
|
initial r_wb_cyc_gbl = 1'b0; |
initial r_wb_cyc_lcl = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
begin |
r_wb_cyc_gbl <= 1'b0; |
r_wb_cyc_lcl <= 1'b0; |
end else if ((r_wb_cyc_gbl)||(r_wb_cyc_lcl)) |
begin |
if ((i_wb_ack)||(i_wb_err)) |
begin |
r_wb_cyc_gbl <= 1'b0; |
r_wb_cyc_lcl <= 1'b0; |
end |
end else if (i_stb) // New memory operation |
begin // Grab the wishbone |
r_wb_cyc_lcl <= lcl_stb; |
r_wb_cyc_gbl <= gbl_stb; |
end |
always @(posedge i_clk) |
if (o_wb_cyc_gbl) |
o_wb_stb_gbl <= (o_wb_stb_gbl)&&(i_wb_stall); |
else |
o_wb_stb_gbl <= gbl_stb; // Grab wishbone on new operation |
always @(posedge i_clk) |
if (o_wb_cyc_lcl) |
o_wb_stb_lcl <= (o_wb_stb_lcl)&&(i_wb_stall); |
else |
o_wb_stb_lcl <= lcl_stb; // Grab wishbone on new operation |
always @(posedge i_clk) |
if (i_stb) |
begin |
o_wb_we <= i_op; |
o_wb_data <= i_data; |
o_wb_addr <= i_addr[(AW-1):0]; |
end |
|
initial o_valid = 1'b0; |
always @(posedge i_clk) |
o_valid <= ((o_wb_cyc_gbl)||(o_wb_cyc_lcl))&&(i_wb_ack)&&(~o_wb_we); |
initial o_err = 1'b0; |
always @(posedge i_clk) |
o_err <= ((o_wb_cyc_gbl)||(o_wb_cyc_lcl))&&(i_wb_err); |
assign o_busy = (o_wb_cyc_gbl)||(o_wb_cyc_lcl); |
|
always @(posedge i_clk) |
if (i_stb) |
o_wreg <= i_oreg; |
always @(posedge i_clk) |
if (i_wb_ack) |
o_result <= i_wb_data; |
|
generate |
if (IMPLEMENT_LOCK != 0) |
begin |
reg lock_gbl, lock_lcl; |
|
initial lock_gbl = 1'b0; |
initial lock_lcl = 1'b0; |
|
always @(posedge i_clk) |
begin |
lock_gbl <= (i_lock)&&((r_wb_cyc_gbl)||(lock_gbl)); |
lock_lcl <= (i_lock)&&((r_wb_cyc_lcl)||(lock_lcl)); |
end |
|
assign o_wb_cyc_gbl = (r_wb_cyc_gbl)||(lock_gbl); |
assign o_wb_cyc_lcl = (r_wb_cyc_lcl)||(lock_lcl); |
end else begin |
assign o_wb_cyc_gbl = (r_wb_cyc_gbl); |
assign o_wb_cyc_lcl = (r_wb_cyc_lcl); |
end endgenerate |
endmodule |
/trunk/rtl/cpu/wbarbiter.v
0,0 → 1,184
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: wbarbiter.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: At some point in time, I might wish to have two masters connect |
// to the same wishbone bus. As an example, I might wish to have |
// both the instruction fetch and the load/store operators |
// of my Zip CPU access the the same bus. How shall they both |
// get access to the same resource? This module allows the |
// wishbone interfaces from two sources to drive the bus, while |
// guaranteeing that only one drives the bus at a time. |
// |
// The core logic works like this: |
// |
// 1. If 'A' or 'B' asserts the o_cyc line, a bus cycle will begin, |
// with acccess granted to whomever requested it. |
// 2. If both 'A' and 'B' assert o_cyc at the same time, only 'A' |
// will be granted the bus. (If the alternating parameter |
// is set, A and B will alternate who gets the bus in |
// this case.) |
// 3. The bus will remain owned by whomever the bus was granted to |
// until they deassert the o_cyc line. |
// 4. At the end of a bus cycle, o_cyc is guaranteed to be |
// deasserted (low) for one clock. |
// 5. On the next clock, bus arbitration takes place again. If |
// 'A' requests the bus, no matter how long 'B' was |
// waiting, 'A' will then be granted the bus. (Unless |
// again the alternating parameter is set, then the |
// access is guaranteed to switch to B.) |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
`define WBA_ALTERNATING |
module wbarbiter(i_clk, i_rst, |
// Bus A |
i_a_adr, i_a_dat, i_a_we, i_a_stb, i_a_cyc, o_a_ack, o_a_stall, o_a_err, |
// Bus B |
i_b_adr, i_b_dat, i_b_we, i_b_stb, i_b_cyc, o_b_ack, o_b_stall, o_b_err, |
// Both buses |
o_adr, o_dat, o_we, o_stb, o_cyc, i_ack, i_stall, i_err); |
// 18 bits will address one GB, 4 bytes at a time. |
// 19 bits will allow the ability to address things other than just |
// the 1GB of memory we are expecting. |
parameter DW=32, AW=19; |
// Wishbone doesn't use an i_ce signal. While it could, they dislike |
// what it would (might) do to the synchronous reset signal, i_rst. |
input i_clk, i_rst; |
input [(AW-1):0] i_a_adr, i_b_adr; |
input [(DW-1):0] i_a_dat, i_b_dat; |
input i_a_we, i_a_stb, i_a_cyc; |
input i_b_we, i_b_stb, i_b_cyc; |
output wire o_a_ack, o_b_ack, o_a_stall, o_b_stall, |
o_a_err, o_b_err; |
output wire [(AW-1):0] o_adr; |
output wire [(DW-1):0] o_dat; |
output wire o_we, o_stb, o_cyc; |
input i_ack, i_stall, i_err; |
|
// All the fancy stuff here is done with the three primary signals: |
// o_cyc |
// w_a_owner |
// w_b_owner |
// These signals are helped by r_cyc, r_a_owner, and r_b_owner. |
// If you understand these signals, all else will fall into place. |
|
// r_cyc just keeps track of the last o_cyc value. That way, on |
// the next clock we can tell if we've had one non-cycle before |
// starting another cycle. Specifically, no new cycles will be |
// allowed to begin unless r_cyc=0. |
reg r_cyc; |
always @(posedge i_clk) |
if (i_rst) |
r_cyc <= 1'b0; |
else |
r_cyc <= o_cyc; |
|
// Go high immediately (new cycle) if ... |
// Previous cycle was low and *someone* is requesting a bus cycle |
// Go low immadiately if ... |
// We were just high and the owner no longer wants the bus |
// WISHBONE Spec recommends no logic between a FF and the o_cyc |
// This violates that spec. (Rec 3.15, p35) |
assign o_cyc = ((~r_cyc)&&((i_a_cyc)||(i_b_cyc))) || ((r_cyc)&&((w_a_owner)||(w_b_owner))); |
|
|
// Register keeping track of the last owner, wire keeping track of the |
// current owner allowing us to not lose a clock in arbitrating the |
// first clock of the bus cycle |
reg r_a_owner, r_b_owner; |
wire w_a_owner, w_b_owner; |
`ifdef WBA_ALTERNATING |
reg r_a_last_owner; |
// Stall must be asserted on the same cycle the input master asserts |
// the bus, if the bus isn't granted to him. |
assign o_a_stall = (w_a_owner) ? i_stall : 1'b1; |
assign o_b_stall = (w_b_owner) ? i_stall : 1'b1; |
|
`endif |
always @(posedge i_clk) |
if (i_rst) |
begin |
r_a_owner <= 1'b0; |
r_b_owner <= 1'b0; |
end else begin |
r_a_owner <= w_a_owner; |
r_b_owner <= w_b_owner; |
`ifdef WBA_ALTERNATING |
if (w_a_owner) |
r_a_last_owner <= 1'b1; |
else if (w_b_owner) |
r_a_last_owner <= 1'b0; |
`endif |
end |
// |
// If you are the owner, retain ownership until i_x_cyc is no |
// longer asserted. Likewise, you cannot become owner until o_cyc |
// is de-asserted for one cycle. |
// |
// 'A' is given arbitrary priority over 'B' |
// 'A' may own the bus only if he wants it. When 'A' drops i_a_cyc, |
// o_cyc must drop and so must w_a_owner on the same cycle. |
// However, when 'A' asserts i_a_cyc, he can only capture the bus if |
// it's had an idle cycle. |
// The same is true for 'B' with one exception: if both contend for the |
// bus on the same cycle, 'A' arbitrarily wins. |
`ifdef WBA_ALTERNATING |
assign w_a_owner = (i_a_cyc) // if A requests ownership, and either |
&& ((r_a_owner) // A has already been recognized or |
|| ((~r_cyc) // the bus is free and |
&&((~i_b_cyc) // B has not requested, or if he |
||(~r_a_last_owner)) )); // has, it's A's turn |
assign w_b_owner = (i_b_cyc)&& ((r_b_owner) || ((~r_cyc)&&((~i_a_cyc)||(r_a_last_owner)) )); |
`else |
assign w_a_owner = (i_a_cyc)&& ((r_a_owner) || (~r_cyc) ); |
assign w_b_owner = (i_b_cyc)&& ((r_b_owner) || ((~r_cyc)&&(~i_a_cyc)) ); |
`endif |
|
// Realistically, if neither master owns the bus, the output is a |
// don't care. Thus we trigger off whether or not 'A' owns the bus. |
// If 'B' owns it all we care is that 'A' does not. Likewise, if |
// neither owns the bus than the values on the various lines are |
// irrelevant. |
assign o_adr = (w_a_owner) ? i_a_adr : i_b_adr; |
assign o_dat = (w_a_owner) ? i_a_dat : i_b_dat; |
assign o_we = (w_a_owner) ? i_a_we : i_b_we; |
assign o_stb = (o_cyc) && ((w_a_owner) ? i_a_stb : i_b_stb); |
|
// We cannot allow the return acknowledgement to ever go high if |
// the master in question does not own the bus. Hence we force it |
// low if the particular master doesn't own the bus. |
assign o_a_ack = (w_a_owner) ? i_ack : 1'b0; |
assign o_b_ack = (w_b_owner) ? i_ack : 1'b0; |
|
// |
// |
assign o_a_err = (w_a_owner) ? i_err : 1'b0; |
assign o_b_err = (w_b_owner) ? i_err : 1'b0; |
|
endmodule |
|
/trunk/rtl/cpu/ziptimer.v
0,0 → 1,135
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: ziptimer.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: A lighter weight implementation of the Zip Timer. |
// |
// Interface: |
// Two options: |
// 1. One combined register for both control and value, and ... |
// The reload value is set any time the timer data value is "set". |
// Reading the register returns the timer value. Controls are |
// set so that writing a value to the timer automatically starts |
// it counting down. |
// 2. Two registers, one for control one for value. |
// The control register would have the reload value in it. |
// On the clock when the interface is set to zero the interrupt is set. |
// Hence setting the timer to zero will disable the timer without |
// setting any interrupts. Thus setting it to five will count |
// 5 clocks: 5, 4, 3, 2, 1, Interrupt. |
// |
// |
// Control bits: |
// (Start_n/Stop. This bit has been dropped. Writing to this |
// timer any value but zero starts it. Writing a zero |
// clears and stops it.) |
// AutoReload. If set, then on reset the timer automatically |
// loads the last set value and starts over. This is |
// useful for distinguishing between a one-time interrupt |
// timer, and a repetitive interval timer. |
// (INTEN. Interrupt enable--reaching zero always creates an |
// interrupt, so this control bit isn't needed. The |
// interrupt controller can be used to mask the interrupt.) |
// (COUNT-DOWN/UP: This timer is *only* a count-down timer. |
// There is no means of setting it to count up.) |
// WatchDog |
// This timer can be implemented as a watchdog timer simply by |
// connecting the interrupt line to the reset line of the CPU. |
// When the timer then expires, it will trigger a CPU reset. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
module ziptimer(i_clk, i_rst, i_ce, |
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, |
o_wb_ack, o_wb_stall, o_wb_data, |
o_int); |
parameter BW = 32, VW = (BW-1); |
input i_clk, i_rst, i_ce; |
// Wishbone inputs |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input [(BW-1):0] i_wb_data; |
// Wishbone outputs |
output reg o_wb_ack; |
output wire o_wb_stall; |
output wire [(BW-1):0] o_wb_data; |
// Interrupt line |
output reg o_int; |
|
reg r_auto_reload, r_running; |
reg [(VW-1):0] r_reload_value; |
|
wire wb_write; |
assign wb_write = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)); |
|
initial r_running = 1'b0; |
initial r_auto_reload = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_running <= 1'b0; |
else if (wb_write) |
r_running <= (|i_wb_data[(VW-1):0]); |
else if ((o_int)&&(~r_auto_reload)) |
r_running <= 1'b0; |
|
|
always @(posedge i_clk) |
if (wb_write) |
r_auto_reload <= (i_wb_data[(BW-1)]); |
|
// If setting auto-reload mode, and the value to other |
// than zero, set the auto-reload value |
always @(posedge i_clk) |
if ((wb_write)&&(i_wb_data[(BW-1)])&&(|i_wb_data[(VW-1):0])) |
r_reload_value <= i_wb_data[(VW-1):0]; |
|
|
reg [(VW-1):0] r_value; |
initial r_value = 0; |
always @(posedge i_clk) |
if (wb_write) |
r_value <= i_wb_data[(VW-1):0]; |
else if ((r_running)&&(i_ce)&&(~o_int)) |
r_value <= r_value + {(VW){1'b1}}; // r_value - 1; |
else if ((r_running)&&(r_auto_reload)&&(o_int)) |
r_value <= r_reload_value; |
|
// Set the interrupt on our last tick. |
initial o_int = 1'b0; |
always @(posedge i_clk) |
if (i_ce) |
o_int <= (r_running)&&(r_value == { {(VW-1){1'b0}}, 1'b1 }); |
else |
o_int <= 1'b0; |
|
initial o_wb_ack = 1'b0; |
always @(posedge i_clk) |
o_wb_ack <= (i_wb_cyc)&&(i_wb_stb); |
assign o_wb_stall = 1'b0; |
|
assign o_wb_data = { r_auto_reload, r_value }; |
|
endmodule |
/trunk/rtl/cpu/busdelay.v
0,0 → 1,101
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: busdelay.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: Delay any access to the wishbone bus by a single clock. |
// |
// When the first Zip System would not meet the timing requirements of |
// the board it was placed upon, this bus delay was added to help out. |
// It may no longer be necessary, having cleaned some other problems up |
// first, but it will remain here as a means of alleviating timing |
// problems. |
// |
// The specific problem takes place on the stall line: a wishbone master |
// *must* know on the first clock whether or not the bus will stall. |
// |
// |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
module busdelay(i_clk, |
// The input bus |
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, |
o_wb_ack, o_wb_stall, o_wb_data, o_wb_err, |
// The delayed bus |
o_dly_cyc, o_dly_stb, o_dly_we, o_dly_addr, o_dly_data, |
i_dly_ack, i_dly_stall, i_dly_data, i_dly_err); |
parameter AW=32, DW=32; |
input i_clk; |
// Input/master bus |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input [(AW-1):0] i_wb_addr; |
input [(DW-1):0] i_wb_data; |
output reg o_wb_ack; |
output wire o_wb_stall; |
output reg [(DW-1):0] o_wb_data; |
output wire o_wb_err; |
// Delayed bus |
output reg o_dly_cyc, o_dly_stb, o_dly_we; |
output reg [(AW-1):0] o_dly_addr; |
output reg [(DW-1):0] o_dly_data; |
input i_dly_ack; |
input i_dly_stall; |
input [(DW-1):0] i_dly_data; |
input i_dly_err; |
|
initial o_dly_cyc = 1'b0; |
initial o_dly_stb = 1'b0; |
|
always @(posedge i_clk) |
o_dly_cyc <= i_wb_cyc; |
// Add the i_wb_cyc criteria here, so we can simplify the o_wb_stall |
// criteria below, which would otherwise *and* these two. |
always @(posedge i_clk) |
if (~o_wb_stall) |
o_dly_stb <= ((i_wb_cyc)&&(i_wb_stb)); |
always @(posedge i_clk) |
if (~o_wb_stall) |
o_dly_we <= i_wb_we; |
always @(posedge i_clk) |
if (~o_wb_stall) |
o_dly_addr<= i_wb_addr; |
always @(posedge i_clk) |
if (~o_wb_stall) |
o_dly_data <= i_wb_data; |
always @(posedge i_clk) |
o_wb_ack <= (i_dly_ack)&&(o_dly_cyc)&&(i_wb_cyc); |
always @(posedge i_clk) |
o_wb_data <= i_dly_data; |
|
// Our only non-delayed line, yet still really delayed. Perhaps |
// there's a way to register this? |
// o_wb_stall <= (i_wb_cyc)&&(i_wb_stb) ... or some such? |
// assign o_wb_stall=((i_wb_cyc)&&(i_dly_stall)&&(o_dly_stb));//&&o_cyc |
assign o_wb_stall = ((i_dly_stall)&&(o_dly_stb));//&&o_cyc |
assign o_wb_err = i_dly_err; |
|
endmodule |
/trunk/rtl/cpu/cpuops.v
0,0 → 1,200
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: cpuops.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: This supports the instruction set reordering of operations |
// created by the second generation instruction set, as well as |
// the new operations of POPC (population count) and BREV (bit reversal). |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
module cpuops(i_clk,i_rst, i_ce, i_valid, i_op, i_a, i_b, o_c, o_f, o_valid, |
o_illegal, o_busy); |
parameter IMPLEMENT_MPY = 1; |
input i_clk, i_rst, i_ce; |
input [3:0] i_op; |
input [31:0] i_a, i_b; |
input i_valid; |
output reg [31:0] o_c; |
output wire [3:0] o_f; |
output reg o_valid; |
output wire o_illegal; |
output wire o_busy; |
|
// Rotate-left pre-logic |
wire [63:0] w_rol_tmp; |
assign w_rol_tmp = { i_a, i_a } << i_b[4:0]; |
wire [31:0] w_rol_result; |
assign w_rol_result = w_rol_tmp[63:32]; // Won't set flags |
|
// Shift register pre-logic |
wire [32:0] w_lsr_result, w_asr_result; |
assign w_asr_result = (|i_b[31:5])? {(33){i_a[31]}} |
: ( {i_a, 1'b0 } >>> (i_b[4:0]) );// ASR |
assign w_lsr_result = (|i_b[31:5])? 33'h00 |
: ( { i_a, 1'b0 } >> (i_b[4:0]) );// LSR |
|
// Bit reversal pre-logic |
wire [31:0] w_brev_result; |
genvar k; |
generate |
for(k=0; k<32; k=k+1) |
begin : bit_reversal_cpuop |
assign w_brev_result[k] = i_b[31-k]; |
end endgenerate |
|
// Popcount pre-logic |
wire [31:0] w_popc_result; |
assign w_popc_result[5:0]= |
({5'h0,i_b[ 0]}+{5'h0,i_b[ 1]}+{5'h0,i_b[ 2]}+{5'h0,i_b[ 3]}) |
+({5'h0,i_b[ 4]}+{5'h0,i_b[ 5]}+{5'h0,i_b[ 6]}+{5'h0,i_b[ 7]}) |
+({5'h0,i_b[ 8]}+{5'h0,i_b[ 9]}+{5'h0,i_b[10]}+{5'h0,i_b[11]}) |
+({5'h0,i_b[12]}+{5'h0,i_b[13]}+{5'h0,i_b[14]}+{5'h0,i_b[15]}) |
+({5'h0,i_b[16]}+{5'h0,i_b[17]}+{5'h0,i_b[18]}+{5'h0,i_b[19]}) |
+({5'h0,i_b[20]}+{5'h0,i_b[21]}+{5'h0,i_b[22]}+{5'h0,i_b[23]}) |
+({5'h0,i_b[24]}+{5'h0,i_b[25]}+{5'h0,i_b[26]}+{5'h0,i_b[27]}) |
+({5'h0,i_b[28]}+{5'h0,i_b[29]}+{5'h0,i_b[30]}+{5'h0,i_b[31]}); |
assign w_popc_result[31:6] = 26'h00; |
|
// Prelogic for our flags registers |
wire z, n, v; |
reg c, pre_sign, set_ovfl; |
always @(posedge i_clk) |
if (i_ce) // 1 LUT |
set_ovfl =(((i_op==4'h0)&&(i_a[31] != i_b[31]))//SUB&CMP |
||((i_op==4'h2)&&(i_a[31] == i_b[31])) // ADD |
||(i_op == 4'h6) // LSL |
||(i_op == 4'h5)); // LSR |
|
|
// A 4-way multiplexer can be done in one 6-LUT. |
// A 16-way multiplexer can therefore be done in 4x 6-LUT's with |
// the Xilinx multiplexer fabric that follows. |
// Given that we wish to apply this multiplexer approach to 33-bits, |
// this will cost a minimum of 132 6-LUTs. |
generate |
if (IMPLEMENT_MPY == 0) |
begin |
always @(posedge i_clk) |
if (i_ce) |
begin |
pre_sign <= (i_a[31]); |
c <= 1'b0; |
casez(i_op) |
4'b0000:{c,o_c } <= {1'b0,i_a}-{1'b0,i_b};// CMP/SUB |
4'b0001: o_c <= i_a & i_b; // BTST/And |
4'b0010:{c,o_c } <= i_a + i_b; // Add |
4'b0011: o_c <= i_a | i_b; // Or |
4'b0100: o_c <= i_a ^ i_b; // Xor |
4'b0101:{o_c,c } <= w_lsr_result[32:0]; // LSR |
4'b0110:{c,o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0]; // LSL |
4'b0111:{o_c,c } <= w_asr_result[32:0]; // ASR |
4'b1000: o_c <= { i_b[15: 0], i_a[15:0] }; // LODIHI |
4'b1001: o_c <= { i_a[31:16], i_b[15:0] }; // LODILO |
// 4'h1010: The unimplemented MPYU, |
// 4'h1011: and here for the unimplemented MPYS |
4'b1100: o_c <= w_brev_result; // BREV |
4'b1101: o_c <= w_popc_result; // POPC |
4'b1110: o_c <= w_rol_result; // ROL |
default: o_c <= i_b; // MOV, LDI |
endcase |
end |
|
assign o_busy = 1'b0; |
|
reg r_illegal; |
always @(posedge i_clk) |
r_illegal <= (i_ce)&&((i_op == 4'h3)||(i_op == 4'h4)); |
assign o_illegal = r_illegal; |
end else begin |
// |
// Multiply pre-logic |
// |
wire signed [16:0] w_mpy_a_input, w_mpy_b_input; |
wire [33:0] w_mpy_result; |
reg [31:0] r_mpy_result; |
assign w_mpy_a_input ={ ((i_a[15])&(i_op[0])), i_a[15:0] }; |
assign w_mpy_b_input ={ ((i_b[15])&(i_op[0])), i_b[15:0] }; |
assign w_mpy_result = w_mpy_a_input * w_mpy_b_input; |
always @(posedge i_clk) |
if (i_ce) |
r_mpy_result = w_mpy_result[31:0]; |
|
// |
// The master ALU case statement |
// |
always @(posedge i_clk) |
if (i_ce) |
begin |
pre_sign <= (i_a[31]); |
c <= 1'b0; |
casez(i_op) |
4'b0000:{c,o_c } <= {1'b0,i_a}-{1'b0,i_b};// CMP/SUB |
4'b0001: o_c <= i_a & i_b; // BTST/And |
4'b0010:{c,o_c } <= i_a + i_b; // Add |
4'b0011: o_c <= i_a | i_b; // Or |
4'b0100: o_c <= i_a ^ i_b; // Xor |
4'b0101:{o_c,c } <= w_lsr_result[32:0]; // LSR |
4'b0110:{c,o_c } <= (|i_b[31:5])? 33'h00 : {1'b0, i_a } << i_b[4:0]; // LSL |
4'b0111:{o_c,c } <= w_asr_result[32:0]; // ASR |
4'b1000: o_c <= { i_b[15: 0], i_a[15:0] }; // LODIHI |
4'b1001: o_c <= { i_a[31:16], i_b[15:0] }; // LODILO |
4'b1010: o_c <= r_mpy_result; // MPYU |
4'b1011: o_c <= r_mpy_result; // MPYS |
4'b1100: o_c <= w_brev_result; // BREV |
4'b1101: o_c <= w_popc_result; // POPC |
4'b1110: o_c <= w_rol_result; // ROL |
default: o_c <= i_b; // MOV, LDI |
endcase |
end else if (r_busy) |
o_c <= r_mpy_result; |
|
reg r_busy; |
initial r_busy = 1'b0; |
always @(posedge i_clk) |
r_busy <= (~i_rst)&&(i_ce)&&(i_valid) |
&&(i_op[3:1] == 3'h5); |
|
assign o_busy = r_busy; |
|
assign o_illegal = 1'b0; |
end endgenerate |
|
assign z = (o_c == 32'h0000); |
assign n = (o_c[31]); |
assign v = (set_ovfl)&&(pre_sign != o_c[31]); |
|
assign o_f = { v, n, c, z }; |
|
initial o_valid = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
o_valid <= 1'b0; |
else |
o_valid <= (i_ce)&&(i_valid)&&(i_op[3:1] != 3'h5) |
||(o_busy); |
endmodule |
/trunk/rtl/cpu/zipbones.v
0,0 → 1,195
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: zipbones.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: In the spirit of keeping the Zip CPU small, this implements a |
// Zip System with no peripherals: Any peripherals you wish will |
// need to be implemented off-module. |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
`include "cpudefs.v" |
// |
module zipbones(i_clk, i_rst, |
// Wishbone master interface from the CPU |
o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, |
i_wb_ack, i_wb_stall, i_wb_data, i_wb_err, |
// Incoming interrupts |
i_ext_int, |
// Our one outgoing interrupt |
o_ext_int, |
// Wishbone slave interface for debugging purposes |
i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data, |
o_dbg_ack, o_dbg_stall, o_dbg_data |
`ifdef DEBUG_SCOPE |
, o_zip_debug |
`endif |
); |
parameter RESET_ADDRESS=32'h0100000, ADDRESS_WIDTH=32, |
LGICACHE=6, START_HALTED=0, |
AW=ADDRESS_WIDTH; |
input i_clk, i_rst; |
// Wishbone master |
output wire o_wb_cyc, o_wb_stb, o_wb_we; |
output wire [(AW-1):0] o_wb_addr; |
output wire [31:0] o_wb_data; |
input i_wb_ack, i_wb_stall; |
input [31:0] i_wb_data; |
input i_wb_err; |
// Incoming interrupts |
input i_ext_int; |
// Outgoing interrupt |
output wire o_ext_int; |
// Wishbone slave |
input i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr; |
input [31:0] i_dbg_data; |
output reg o_dbg_ack; |
output wire o_dbg_stall; |
output wire [31:0] o_dbg_data; |
// |
`ifdef DEBUG_SCOPE |
output wire [31:0] o_zip_debug; |
`endif |
|
// |
// |
// |
wire sys_cyc, sys_stb, sys_we; |
wire [4:0] sys_addr; |
wire [(AW-1):0] cpu_addr; |
wire [31:0] sys_data; |
wire sys_ack, sys_stall; |
|
// |
// The external debug interface |
// |
// We offer only a limited interface here, requiring a pre-register |
// write to set the local address. This interface allows access to |
// the Zip System on a debug basis only, and not to the rest of the |
// wishbone bus. Further, to access these registers, the control |
// register must first be accessed to both stop the CPU and to |
// set the following address in question. Hence all accesses require |
// two accesses: write the address to the control register (and halt |
// the CPU if not halted), then read/write the data from the data |
// register. |
// |
wire cpu_break, dbg_cmd_write; |
reg cmd_reset, cmd_halt, cmd_step, cmd_clear_pf_cache; |
reg [4:0] cmd_addr; |
wire [3:0] cpu_dbg_cc; |
assign dbg_cmd_write = (i_dbg_cyc)&&(i_dbg_stb)&&(i_dbg_we)&&(~i_dbg_addr); |
// |
// Always start us off with an initial reset |
// |
initial cmd_reset = 1'b1; |
always @(posedge i_clk) |
cmd_reset <= ((dbg_cmd_write)&&(i_dbg_data[6])); |
// |
initial cmd_halt = START_HALTED; |
always @(posedge i_clk) |
if (i_rst) |
cmd_halt <= (START_HALTED == 1)? 1'b1 : 1'b0; |
else if (dbg_cmd_write) |
cmd_halt <= ((i_dbg_data[10])||(i_dbg_data[8])); |
else if ((cmd_step)||(cpu_break)) |
cmd_halt <= 1'b1; |
|
initial cmd_clear_pf_cache = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
cmd_clear_pf_cache <= 1'b0; |
else if (dbg_cmd_write) |
cmd_clear_pf_cache <= i_dbg_data[11]; |
else |
cmd_clear_pf_cache <= 1'b0; |
// |
initial cmd_step = 1'b0; |
always @(posedge i_clk) |
cmd_step <= (dbg_cmd_write)&&(i_dbg_data[8]); |
// |
initial cmd_addr = 5'h0; |
always @(posedge i_clk) |
if (dbg_cmd_write) |
cmd_addr <= i_dbg_data[4:0]; |
|
wire cpu_reset; |
assign cpu_reset = (cmd_reset)||(i_rst); |
|
wire cpu_halt, cpu_dbg_stall; |
assign cpu_halt = (i_rst)||((cmd_halt)&&(~cmd_step)); |
wire [31:0] cmd_data; |
// Values: |
// 0x0003f -> cmd_addr mask |
// 0x00040 -> reset |
// 0x00080 -> PIC interrrupts enabled |
// 0x00100 -> cmd_step |
// 0x00200 -> cmd_stall |
// 0x00400 -> cmd_halt |
// 0x00800 -> cmd_clear_pf_cache |
// 0x01000 -> cc.sleep |
// 0x02000 -> cc.gie |
// 0x10000 -> External interrupt line is high |
assign cmd_data = { 7'h00, 8'h00, i_ext_int, |
cpu_dbg_cc, |
1'b0, cmd_halt, (~cpu_dbg_stall), 1'b0, |
1'b0, cpu_reset, 1'b0, cmd_addr }; |
|
// |
// The CPU itself |
// |
wire cpu_gbl_stb, cpu_lcl_cyc, cpu_lcl_stb, |
cpu_we, cpu_dbg_we, |
cpu_op_stall, cpu_pf_stall, cpu_i_count; |
wire [31:0] cpu_data; |
wire [31:0] cpu_dbg_data; |
assign cpu_dbg_we = ((i_dbg_cyc)&&(i_dbg_stb) |
&&(i_dbg_we)&&(i_dbg_addr)); |
zipcpu #(RESET_ADDRESS,ADDRESS_WIDTH,LGICACHE) |
thecpu(i_clk, cpu_reset, i_ext_int, |
cpu_halt, cmd_clear_pf_cache, cmd_addr[4:0], cpu_dbg_we, |
i_dbg_data, cpu_dbg_stall, cpu_dbg_data, |
cpu_dbg_cc, cpu_break, |
o_wb_cyc, o_wb_stb, |
cpu_lcl_cyc, cpu_lcl_stb, |
o_wb_we, o_wb_addr, o_wb_data, |
i_wb_ack, i_wb_stall, i_wb_data, |
(i_wb_err)||((cpu_lcl_cyc)&&(cpu_lcl_stb)), |
cpu_op_stall, cpu_pf_stall, cpu_i_count |
`ifdef DEBUG_SCOPE |
, o_zip_debug |
`endif |
); |
|
// Return debug response values |
assign o_dbg_data = (~i_dbg_addr)?cmd_data :cpu_dbg_data; |
initial o_dbg_ack = 1'b0; |
always @(posedge i_clk) |
o_dbg_ack <= (i_dbg_cyc)&&((~i_dbg_addr)||(~o_dbg_stall)); |
assign o_dbg_stall=(i_dbg_cyc)&&(cpu_dbg_stall)&&(i_dbg_addr); |
|
assign o_ext_int = (cmd_halt) && (~i_wb_stall); |
|
endmodule |
/trunk/rtl/cpu/icontrol.v
0,0 → 1,153
//////////////////////////////////////////////////////////////////////////////// |
// |
// Filename: icontrol.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: An interrupt controller, for managing many interrupt sources. |
// |
// This interrupt controller started from the question of how best to |
// design a simple interrupt controller. As such, it has a few nice |
// qualities to it: |
// 1. This is wishbone compliant |
// 2. It sits on a 32-bit wishbone data bus |
// 3. It only consumes one address on that wishbone bus. |
// 4. There is no extra delays associated with reading this |
// device. |
// 5. Common operations can all be done in one clock. |
// |
// So, how shall this be used? First, the 32-bit word is broken down as |
// follows: |
// |
// Bit 31 - This is the global interrupt enable bit. If set, interrupts |
// will be generated and passed on as they come in. |
// Bits 16-30 - These are specific interrupt enable lines. If set, |
// interrupts from source (bit#-16) will be enabled. |
// To set this line and enable interrupts from this source, write |
// to the register with this bit set and the global enable set. |
// To disable this line, write to this register with global enable |
// bit not set, but this bit set. (Writing a zero to any of these |
// bits has no effect, either setting or unsetting them.) |
// Bit 15 - This is the any interrupt pin. If any interrupt is pending, |
// this bit will be set. |
// Bits 0-14 - These are interrupt bits. When set, an interrupt is |
// pending from the corresponding source--regardless of whether |
// it was enabled. (If not enabled, it won't generate an |
// interrupt, but it will still register here.) To clear any |
// of these bits, write a '1' to the corresponding bit. Writing |
// a zero to any of these bits has no effect. |
// |
// The peripheral also sports a parameter, IUSED, which can be set |
// to any value between 1 and (buswidth/2-1, or) 15 inclusive. This will |
// be the number of interrupts handled by this routine. (Without the |
// parameter, Vivado was complaining about unused bits. With it, we can |
// keep the complaints down and still use the routine). |
// |
// To get access to more than 15 interrupts, chain these together, so |
// that one interrupt controller device feeds another. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
//////////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
//////////////////////////////////////////////////////////////////////////////// |
// |
module icontrol(i_clk, i_reset, i_wr, i_proc_bus, o_proc_bus, |
i_brd_ints, o_interrupt); |
parameter IUSED = 15; |
input i_clk, i_reset; |
input i_wr; |
input [31:0] i_proc_bus; |
output wire [31:0] o_proc_bus; |
input [(IUSED-1):0] i_brd_ints; |
output wire o_interrupt; |
|
reg [(IUSED-1):0] r_int_state; |
reg [(IUSED-1):0] r_int_enable; |
wire [(IUSED-1):0] nxt_int_state; |
reg r_any, r_interrupt, r_gie; |
|
assign nxt_int_state = (r_int_state|i_brd_ints); |
initial r_int_state = 0; |
always @(posedge i_clk) |
if (i_reset) |
r_int_state <= 0; |
else if (i_wr) |
r_int_state <= nxt_int_state & (~i_proc_bus[(IUSED-1):0]); |
else |
r_int_state <= nxt_int_state; |
initial r_int_enable = 0; |
always @(posedge i_clk) |
if (i_reset) |
r_int_enable <= 0; |
else if ((i_wr)&&(i_proc_bus[31])) |
r_int_enable <= r_int_enable | i_proc_bus[(16+IUSED-1):16]; |
else if ((i_wr)&&(~i_proc_bus[31])) |
r_int_enable <= r_int_enable & (~ i_proc_bus[(16+IUSED-1):16]); |
|
initial r_gie = 1'b0; |
always @(posedge i_clk) |
if (i_reset) |
r_gie <= 1'b0; |
else if (i_wr) |
r_gie <= i_proc_bus[31]; |
|
initial r_any = 1'b0; |
always @(posedge i_clk) |
r_any <= ((r_int_state & r_int_enable) != 0); |
initial r_interrupt = 1'b0; |
always @(posedge i_clk) |
r_interrupt <= r_gie & r_any; |
|
generate |
if (IUSED < 15) |
begin |
assign o_proc_bus = { |
r_gie, { {(15-IUSED){1'b0}}, r_int_enable }, |
r_any, { {(15-IUSED){1'b0}}, r_int_state } }; |
end else begin |
assign o_proc_bus = { r_gie, r_int_enable, r_any, r_int_state }; |
end endgenerate |
|
/* |
reg int_condition; |
initial int_condition = 1'b0; |
initial o_interrupt_strobe = 1'b0; |
always @(posedge i_clk) |
if (i_reset) |
begin |
int_condition <= 1'b0; |
o_interrupt_strobe <= 1'b0; |
end else if (~r_interrupt) // This might end up generating |
begin // many, many, (wild many) interrupts |
int_condition <= 1'b0; |
o_interrupt_strobe <= 1'b0; |
end else if ((~int_condition)&&(r_interrupt)) |
begin |
int_condition <= 1'b1; |
o_interrupt_strobe <= 1'b1; |
end else |
o_interrupt_strobe <= 1'b0; |
*/ |
|
assign o_interrupt = r_interrupt; |
|
endmodule |
/trunk/rtl/cpu/cpudefs.v
0,0 → 1,271
/////////////////////////////////////////////////////////////////////////////// |
// |
// Filename: cpudefs.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: Some architectures have some needs, others have other needs. |
// Some of my projects need a Zip CPU with pipelining, others |
// can't handle the timing required to get the answer from the ALU |
// back into the input for the ALU. As each different projects has |
// different needs, I can either 1) reconfigure my entire baseline prior |
// to building each project, or 2) host a configuration file which contains |
// the information regarding each baseline. This file is that |
// configuration file. It controls how the CPU (not the system, |
// peripherals, or other) is defined and implemented. Several options |
// are available within here, making the Zip CPU pipelined or not, |
// able to handle a faster clock with more stalls or a slower clock with |
// no stalls, etc. |
// |
// This file encapsulates those control options. |
// |
// The number of LUTs the Zip CPU uses varies dramatically with the |
// options defined in this file. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////////// |
`ifndef CPUDEFS_H |
`define CPUDEFS_H |
// |
// |
// The first couple options control the Zip CPU instruction set, and how |
// it handles various instructions within the set: |
// |
// |
// OPT_ILLEGAL_INSTRUCTION is part of a new section of code that is supposed |
// to recognize illegal instructions and interrupt the CPU whenever one such |
// instruction is encountered. The goal is to create a soft floating point |
// unit via this approach, that can then be replaced with a true floating point |
// unit. As I'm not there yet, it just catches illegal instructions and |
// interrupts the CPU on any such instruction--when defined. Otherwise, |
// illegal instructions are quietly ignored and their behaviour is ... |
// undefined. (Many get treated like NOOPs ...) |
// |
// I recommend setting this flag, although it can be taken out if area is |
// critical ... |
// |
`define OPT_ILLEGAL_INSTRUCTION |
// |
// |
// |
// OPT_MULTIPLY controls whether or not the multiply is built and included |
// in the ALU by default. Set this option and a parameter will be set that |
// includes the multiply. (This parameter may still be overridden, as with |
// any parameter ...) If the multiply is not included and |
// OPT_ILLEGAL_INSTRUCTION is set, then the multiply will create an illegal |
// instruction that will then trip the illegal instruction trap. |
// |
// |
`define OPT_MULTIPLY |
// |
// |
// |
// OPT_DIVIDE controls whether or not the divide instruction is built and |
// included into the ZipCPU by default. Set this option and a parameter will |
// be set that causes the divide unit to be included. (This parameter may |
// still be overridden, as with any parameter ...) If the divide is not |
// included and OPT_ILLEGAL_INSTRUCTION is set, then the multiply will create |
// an illegal instruction exception that will send the CPU into supervisor |
// mode. |
// |
// |
// `define OPT_DIVIDE |
// |
// |
// |
// OPT_IMPLEMENT_FPU will (one day) control whether or not the floating point |
// unit (once I have one) is built and included into the ZipCPU by default. |
// At that time, if this option is set then a parameter will be set that |
// causes the floating point unit to be included. (This parameter may |
// still be overridden, as with any parameter ...) If the floating point unit |
// is not included and OPT_ILLEGAL_INSTRUCTION is set, then as with the |
// multiply and divide any floating point instruction will result in an illegal |
// instruction exception that will send the CPU into supervisor mode. |
// |
// |
// `define OPT_IMPLEMENT_FPU |
// |
// |
// |
// OPT_NEW_INSTRUCTION_SET controls whether or not the new instruction set |
// is in use. The new instruction set contains space for floating point |
// operations, signed and unsigned divide instructions, as well as bit reversal |
// and ... at least two other operations yet to be defined. The decoder alone |
// uses about 70 fewer LUTs, although in practice this works out to 12 fewer |
// when all works out in the wash. Further, floating point and divide |
// instructions will cause an illegal instruction exception if they are not |
// implemented--so software capability can be built to use these instructions |
// immediately, even if the hardware is not yet ready. |
// |
// This option is likely to go away in the future, obsoleting the previous |
// instruction set, so I recommend setting this option and switching to the |
// new instruction set as soon as possible. |
// |
`define OPT_NEW_INSTRUCTION_SET |
// |
// |
// |
// |
// |
// |
// OPT_SINGLE_FETCH controls whether or not the prefetch has a cache, and |
// whether or not it can issue one instruction per clock. When set, the |
// prefetch has no cache, and only one instruction is fetched at a time. |
// This effectively sets the CPU so that only one instruction is ever |
// in the pipeline at once, and hence you may think of this as a "kill |
// pipeline" option. However, since the pipelined fetch component uses so |
// much area on the FPGA, this is an important option to use in trimming down |
// used area if necessary. Hence, it needs to be maintained for that purpose. |
// Be aware, though, it will drop your performance by a factor between 2x and |
// 3x. |
// |
// We can either pipeline our fetches, or issue one fetch at a time. Pipelined |
// fetches are more complicated and therefore use more FPGA resources, while |
// single fetches will cause the CPU to stall for about 5 stalls each |
// instruction cycle, effectively reducing the instruction count per clock to |
// about 0.2. However, the area cost may be worth it. Consider: |
// |
// Slice LUTs ZipSystem ZipCPU |
// Single Fetching 2521 1734 |
// Pipelined fetching 2796 2046 |
// (These numbers may be dated, but should still be representative ...) |
// |
// I recommend only defining this if you "need" to, if area is tight and |
// speed isn't as important. Otherwise, just leave this undefined. |
// |
`define OPT_SINGLE_FETCH |
// |
// |
// |
// The next several options are pipeline optimization options. They make no |
// sense in a single instruction fetch mode, hence we #ifndef them so they |
// are only defined if we are in a full pipelined mode (i.e. OPT_SINGLE_FETCH |
// is not defined). |
// |
`ifndef OPT_SINGLE_FETCH |
// |
// |
// |
// OPT_PIPELINED is the natural result and opposite of using the single |
// instruction fetch unit. If you are not using that unit, the ZipCPU will |
// be pipelined. The option is defined here more for readability than |
// anything else, since OPT_PIPELINED makes more sense than OPT_SINGLE_FETCH, |
// well ... that and it does a better job of explaining what is going on. |
// |
// In other words, leave this define alone--lest you break the ZipCPU. |
// |
`define OPT_PIPELINED |
// |
// |
// |
// OPT_TRADITIONAL_PFCACHE allows you to switch between one of two prefetch |
// caches. If enabled, a more traditional cache is implemented. This more |
// traditional cache (currently) uses many more LUTs, but it also reduces |
// the stall count tremendously over the alternative hacked pipeline cache. |
// (The traditional pfcache is also pipelined, whereas the pipeline cache |
// implements a windowed approach to caching.) |
// |
// If you have the fabric to support this option, I recommend including it. |
// |
// `define OPT_TRADITIONAL_PFCACHE |
// |
// |
// |
// OPT_EARLY_BRANCHING is an attempt to execute a BRA statement as early |
// as possible, to avoid as many pipeline stalls on a branch as possible. |
// It's not tremendously successful yet--BRA's still suffer stalls, |
// but I intend to keep working on this approach until the number of stalls |
// gets down to one or (ideally) zero. (With the OPT_TRADITIONAL_PFCACHE, this |
// gets down to a single stall cycle ...) That way a "BRA" can be used as the |
// compiler's branch prediction optimizer: BRA's barely stall, while branches |
// on conditions will always suffer about 4 stall cycles or so. |
// |
// I recommend setting this flag, so as to turn early branching on. |
// |
`define OPT_EARLY_BRANCHING |
// |
// |
// |
// OPT_PIPELINED_BUS_ACCESS controls whether or not LOD/STO instructions |
// can take advantaged of pipelined bus instructions. To be eligible, the |
// operations must be identical (cannot pipeline loads and stores, just loads |
// only or stores only), and the addresses must either be identical or one up |
// from the previous address. Further, the load/store string must all have |
// the same conditional. This approach gains the must use, in my humble |
// opinion, when saving registers to or restoring registers from the stack |
// at the beginning/end of a procedure, or when doing a context swap. |
// |
// I recommend setting this flag, for performance reasons, especially if your |
// wishbone bus can handle pipelined bus accesses. |
// |
`define OPT_PIPELINED_BUS_ACCESS |
// |
// |
// |
`ifdef OPT_NEW_INSTRUCTION_SET |
// |
// |
// |
// The new instruction set also defines a set of very long instruction words. |
// Well, calling them "very long" instruction words is probably a misnomer, |
// although we're going to do it. They're really 2x16-bit instructions--- |
// instruction words that pack two instructions into one word. (2x14 bit |
// really--'cause you need a bit to note the instruction is a 2x instruction, |
// and then 3-bits for the condition codes ...) Set OPT_VLIW to include these |
// double instructions as part of the new instruction set. These allow a single |
// instruction to contain two instructions within. These instructions are |
// designed to get more code density from the instruction set, and to hopefully |
// take some pain off of the performance of the pre-fetch and instruction cache. |
// |
// These new instructions, however, also necessitate a change in the Zip |
// CPU--the Zip CPU can no longer execute instructions atomically. It must |
// now execute non-VLIW instructions, or VLIW instruction pairs, atomically. |
// This logic has been added into the ZipCPU, but it has not (yet) been |
// tested thoroughly. |
// |
// Oh, and the assembler, the debugger, and the object file dumper, and the |
// simulator all need to be updated as well .... |
// |
`define OPT_VLIW |
// |
// |
`endif // OPT_NEW_INSTRUCTION_SET |
// |
// |
`endif // OPT_SINGLE_FETCH |
// |
// |
// |
// Now let's talk about peripherals for a moment. These next two defines |
// control whether the DMA controller is included in the Zip System, and |
// whether or not the 8 accounting timers are also included. Set these to |
// include the respective peripherals, comment them out not to. |
// |
// `define INCLUDE_DMA_CONTROLLER |
// `define INCLUDE_ACCOUNTING_COUNTERS |
// |
// |
// `define DEBUG_SCOPE |
// |
`endif // CPUDEFS_H |
/trunk/rtl/cpu/zipcpu.v
0,0 → 1,1660
/////////////////////////////////////////////////////////////////////////////// |
// |
// Filename: zipcpu.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: This is the top level module holding the core of the Zip CPU |
// together. The Zip CPU is designed to be as simple as possible. |
// (actual implementation aside ...) The instruction set is about as |
// RISC as you can get, there are only 16 instruction types supported. |
// Please see the accompanying spec.pdf file for a description of these |
// instructions. |
// |
// All instructions are 32-bits wide. All bus accesses, both address and |
// data, are 32-bits over a wishbone bus. |
// |
// The Zip CPU is fully pipelined with the following pipeline stages: |
// |
// 1. Prefetch, returns the instruction from memory. |
// |
// 2. Instruction Decode |
// |
// 3. Read Operands |
// |
// 4. Apply Instruction |
// |
// 4. Write-back Results |
// |
// Further information about the inner workings of this CPU may be |
// found in the spec.pdf file. (The documentation within this file |
// had become out of date and out of sync with the spec.pdf, so look |
// to the spec.pdf for accurate and up to date information.) |
// |
// |
// In general, the pipelining is controlled by three pieces of logic |
// per stage: _ce, _stall, and _valid. _valid means that the stage |
// holds a valid instruction. _ce means that the instruction from the |
// previous stage is to move into this one, and _stall means that the |
// instruction from the previous stage may not move into this one. |
// The difference between these control signals allows individual stages |
// to propagate instructions independently. In general, the logic works |
// as: |
// |
// |
// assign (n)_ce = (n-1)_valid && (~(n)_stall) |
// |
// |
// always @(posedge i_clk) |
// if ((i_rst)||(clear_pipeline)) |
// (n)_valid = 0 |
// else if (n)_ce |
// (n)_valid = 1 |
// else if (n+1)_ce |
// (n)_valid = 0 |
// |
// assign (n)_stall = ( (n-1)_valid && ( pipeline hazard detection ) ) |
// || ( (n)_valid && (n+1)_stall ); |
// |
// and ... |
// |
// always @(posedge i_clk) |
// if (n)_ce |
// (n)_variable = ... whatever logic for this stage |
// |
// Note that a stage can stall even if no instruction is loaded into |
// it. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////////// |
// |
// We can either pipeline our fetches, or issue one fetch at a time. Pipelined |
// fetches are more complicated and therefore use more FPGA resources, while |
// single fetches will cause the CPU to stall for about 5 stalls each |
// instruction cycle, effectively reducing the instruction count per clock to |
// about 0.2. However, the area cost may be worth it. Consider: |
// |
// Slice LUTs ZipSystem ZipCPU |
// Single Fetching 2521 1734 |
// Pipelined fetching 2796 2046 |
// |
// |
// |
`define CPU_CC_REG 4'he |
`define CPU_PC_REG 4'hf |
`define CPU_FPUERR_BIT 12 // Floating point error flag, set on error |
`define CPU_DIVERR_BIT 11 // Divide error flag, set on divide by zero |
`define CPU_BUSERR_BIT 10 // Bus error flag, set on error |
`define CPU_TRAP_BIT 9 // User TRAP has taken place |
`define CPU_ILL_BIT 8 // Illegal instruction |
`define CPU_BREAK_BIT 7 |
`define CPU_STEP_BIT 6 // Will step one or two (VLIW) instructions |
`define CPU_GIE_BIT 5 |
`define CPU_SLEEP_BIT 4 |
// Compile time defines |
// |
`include "cpudefs.v" |
// |
// |
module zipcpu(i_clk, i_rst, i_interrupt, |
// Debug interface |
i_halt, i_clear_pf_cache, i_dbg_reg, i_dbg_we, i_dbg_data, |
o_dbg_stall, o_dbg_reg, o_dbg_cc, |
o_break, |
// CPU interface to the wishbone bus |
o_wb_gbl_cyc, o_wb_gbl_stb, |
o_wb_lcl_cyc, o_wb_lcl_stb, |
o_wb_we, o_wb_addr, o_wb_data, |
i_wb_ack, i_wb_stall, i_wb_data, |
i_wb_err, |
// Accounting/CPU usage interface |
o_op_stall, o_pf_stall, o_i_count |
`ifdef DEBUG_SCOPE |
, o_debug |
`endif |
); |
parameter RESET_ADDRESS=32'h0100000, ADDRESS_WIDTH=24, |
LGICACHE=6; |
`ifdef OPT_MULTIPLY |
parameter IMPLEMENT_MPY = 1; |
`else |
parameter IMPLEMENT_MPY = 0; |
`endif |
`ifdef OPT_DIVIDE |
parameter IMPLEMENT_DIVIDE = 1; |
`else |
parameter IMPLEMENT_DIVIDE = 0; |
`endif |
`ifdef OPT_IMPLEMENT_FPU |
parameter IMPLEMENT_FPU = 1, |
`else |
parameter IMPLEMENT_FPU = 0, |
`endif |
IMPLEMENT_LOCK=1; |
`ifdef OPT_EARLY_BRANCHING |
parameter EARLY_BRANCHING = 1; |
`else |
parameter EARLY_BRANCHING = 0; |
`endif |
parameter AW=ADDRESS_WIDTH; |
input i_clk, i_rst, i_interrupt; |
// Debug interface -- inputs |
input i_halt, i_clear_pf_cache; |
input [4:0] i_dbg_reg; |
input i_dbg_we; |
input [31:0] i_dbg_data; |
// Debug interface -- outputs |
output reg o_dbg_stall; |
output reg [31:0] o_dbg_reg; |
output reg [3:0] o_dbg_cc; |
output wire o_break; |
// Wishbone interface -- outputs |
output wire o_wb_gbl_cyc, o_wb_gbl_stb; |
output wire o_wb_lcl_cyc, o_wb_lcl_stb, o_wb_we; |
output wire [(AW-1):0] o_wb_addr; |
output wire [31:0] o_wb_data; |
// Wishbone interface -- inputs |
input i_wb_ack, i_wb_stall; |
input [31:0] i_wb_data; |
input i_wb_err; |
// Accounting outputs ... to help us count stalls and usage |
output wire o_op_stall; |
output wire o_pf_stall; |
output wire o_i_count; |
// |
`ifdef DEBUG_SCOPE |
output reg [31:0] o_debug; |
`endif |
|
|
// Registers |
// |
// The distributed RAM style comment is necessary on the |
// SPARTAN6 with XST to prevent XST from oversimplifying the register |
// set and in the process ruining everything else. It basically |
// optimizes logic away, to where it no longer works. The logic |
// as described herein will work, this just makes sure XST implements |
// that logic. |
// |
(* ram_style = "distributed" *) |
reg [31:0] regset [0:31]; |
|
// Condition codes |
// (BUS, TRAP,ILL,BREAKEN,STEP,GIE,SLEEP ), V, N, C, Z |
reg [3:0] flags, iflags; |
wire [13:0] w_uflags, w_iflags; |
reg trap, break_en, step, gie, sleep; |
`ifdef OPT_ILLEGAL_INSTRUCTION |
reg ill_err_u, ill_err_i; |
`else |
wire ill_err_u, ill_err_i; |
`endif |
reg ibus_err_flag, ubus_err_flag; |
wire idiv_err_flag, udiv_err_flag; |
wire ifpu_err_flag, ufpu_err_flag; |
wire ihalt_phase, uhalt_phase; |
|
// The master chip enable |
wire master_ce; |
|
// |
// |
// PIPELINE STAGE #1 :: Prefetch |
// Variable declarations |
// |
reg [(AW-1):0] pf_pc; |
reg new_pc; |
wire clear_pipeline; |
assign clear_pipeline = new_pc || i_clear_pf_cache; |
|
wire dcd_stalled; |
wire pf_cyc, pf_stb, pf_we, pf_busy, pf_ack, pf_stall, pf_err; |
wire [(AW-1):0] pf_addr; |
wire [31:0] pf_data; |
wire [31:0] instruction; |
wire [(AW-1):0] instruction_pc; |
wire pf_valid, instruction_gie, pf_illegal; |
|
// |
// |
// PIPELINE STAGE #2 :: Instruction Decode |
// Variable declarations |
// |
// |
reg opvalid, opvalid_mem, opvalid_alu; |
reg opvalid_div, opvalid_fpu; |
wire op_stall, dcd_ce, dcd_phase; |
wire [3:0] dcdOp; |
wire [4:0] dcdA, dcdB, dcdR; |
wire dcdA_cc, dcdB_cc, dcdA_pc, dcdB_pc, dcdR_cc, dcdR_pc; |
wire [3:0] dcdF; |
wire dcdR_wr, dcdA_rd, dcdB_rd, |
dcdALU, dcdM, dcdDV, dcdFP, |
dcdF_wr, dcd_gie, dcd_break, dcd_lock, |
dcd_pipe, dcd_ljmp; |
reg r_dcdvalid; |
wire dcdvalid; |
wire [(AW-1):0] dcd_pc; |
wire [31:0] dcdI; |
wire dcd_zI; // true if dcdI == 0 |
wire dcdA_stall, dcdB_stall, dcdF_stall; |
|
wire dcd_illegal; |
wire dcd_early_branch; |
wire [(AW-1):0] dcd_branch_pc; |
|
|
// |
// |
// PIPELINE STAGE #3 :: Read Operands |
// Variable declarations |
// |
// |
// |
// Now, let's read our operands |
reg [4:0] alu_reg; |
reg [3:0] opn; |
reg [4:0] opR; |
reg [31:0] r_opA, r_opB; |
reg [(AW-1):0] op_pc; |
wire [31:0] w_opA, w_opB; |
wire [31:0] opA_nowait, opB_nowait, opA, opB; |
reg opR_wr, opR_cc, opF_wr, op_gie; |
wire [13:0] opFl; |
reg [5:0] r_opF; |
wire [7:0] opF; |
wire op_ce, op_phase; |
// Some pipeline control wires |
`ifdef OPT_PIPELINED |
reg opA_alu, opA_mem; |
reg opB_alu, opB_mem; |
`endif |
`ifdef OPT_ILLEGAL_INSTRUCTION |
reg op_illegal; |
`endif |
reg op_break; |
wire op_lock; |
|
|
// |
// |
// PIPELINE STAGE #4 :: ALU / Memory |
// Variable declarations |
// |
// |
reg [(AW-1):0] alu_pc; |
reg alu_pc_valid; |
wire alu_phase; |
wire alu_ce, alu_stall; |
wire [31:0] alu_result; |
wire [3:0] alu_flags; |
wire alu_valid, alu_busy; |
wire set_cond; |
reg alu_wr, alF_wr, alu_gie; |
wire alu_illegal_op; |
wire alu_illegal; |
|
|
|
wire mem_ce, mem_stalled; |
`ifdef OPT_PIPELINED_BUS_ACCESS |
wire mem_pipe_stalled; |
`endif |
wire mem_valid, mem_ack, mem_stall, mem_err, bus_err, |
mem_cyc_gbl, mem_cyc_lcl, mem_stb_gbl, mem_stb_lcl, mem_we; |
wire [4:0] mem_wreg; |
|
wire mem_busy, mem_rdbusy; |
wire [(AW-1):0] mem_addr; |
wire [31:0] mem_data, mem_result; |
|
wire div_ce, div_error, div_busy, div_valid; |
wire [31:0] div_result; |
wire [3:0] div_flags; |
|
assign div_ce = (master_ce)&&(~clear_pipeline)&&(opvalid_div) |
&&(~mem_rdbusy)&&(~div_busy)&&(~fpu_busy) |
&&(set_cond); |
|
wire fpu_ce, fpu_error, fpu_busy, fpu_valid; |
wire [31:0] fpu_result; |
wire [3:0] fpu_flags; |
|
assign fpu_ce = (master_ce)&&(~clear_pipeline)&&(opvalid_fpu) |
&&(~mem_rdbusy)&&(~div_busy)&&(~fpu_busy) |
&&(set_cond); |
|
|
// |
// |
// PIPELINE STAGE #5 :: Write-back |
// Variable declarations |
// |
wire wr_reg_ce, wr_flags_ce, wr_write_pc, wr_write_cc; |
wire [4:0] wr_reg_id; |
wire [31:0] wr_reg_vl; |
wire w_switch_to_interrupt, w_release_from_interrupt; |
reg [(AW-1):0] upc, ipc; |
|
|
|
// |
// MASTER: clock enable. |
// |
assign master_ce = (~i_halt)&&(~o_break)&&(~sleep); |
|
|
// |
// PIPELINE STAGE #1 :: Prefetch |
// Calculate stall conditions |
// |
// These are calculated externally, within the prefetch module. |
// |
|
// |
// PIPELINE STAGE #2 :: Instruction Decode |
// Calculate stall conditions |
`ifdef OPT_PIPELINED |
assign dcd_ce = ((~dcdvalid)||(~dcd_stalled))&&(~clear_pipeline); |
`else |
assign dcd_ce = 1'b1; |
`endif |
`ifdef OPT_PIPELINED |
assign dcd_stalled = (dcdvalid)&&(op_stall); |
`else |
// If not pipelined, there will be no opvalid_ anything, and the |
// op_stall will be false, dcdX_stall will be false, thus we can simply |
// do a ... |
assign dcd_stalled = 1'b0; |
`endif |
// |
// PIPELINE STAGE #3 :: Read Operands |
// Calculate stall conditions |
wire op_lock_stall; |
`ifdef OPT_PIPELINED |
assign op_stall = (opvalid)&&( // Only stall if we're loaded w/validins |
// Stall if we're stopped, and not allowed to execute |
// an instruction |
// (~master_ce) // Already captured in alu_stall |
// |
// Stall if going into the ALU and the ALU is stalled |
// i.e. if the memory is busy, or we are single |
// stepping. This also includes our stalls for |
// op_break and op_lock, so we don't need to |
// include those as well here. |
// This also includes whether or not the divide or |
// floating point units are busy. |
(alu_stall) |
// |
// Stall if we are going into memory with an operation |
// that cannot be pipelined, and the memory is |
// already busy |
||(mem_stalled) // &&(opvalid_mem) part of mem_stalled |
) |
||(dcdvalid)&&( |
// Stall if we need to wait for an operand A |
// to be ready to read |
(dcdA_stall) |
// Likewise for B, also includes logic |
// regarding immediate offset (register must |
// be in register file if we need to add to |
// an immediate) |
||(dcdB_stall) |
// Or if we need to wait on flags to work on the |
// CC register |
||(dcdF_stall) |
); |
assign op_ce = ((dcdvalid)||(dcd_illegal))&&(~op_stall)&&(~clear_pipeline); |
`else |
assign op_stall = (opvalid)&&(~master_ce); |
assign op_ce = ((dcdvalid)||(dcd_illegal)); |
`endif |
|
// |
// PIPELINE STAGE #4 :: ALU / Memory |
// Calculate stall conditions |
// |
// 1. Basic stall is if the previous stage is valid and the next is |
// busy. |
// 2. Also stall if the prior stage is valid and the master clock enable |
// is de-selected |
// 3. Stall if someone on the other end is writing the CC register, |
// since we don't know if it'll put us to sleep or not. |
// 4. Last case: Stall if we would otherwise move a break instruction |
// through the ALU. Break instructions are not allowed through |
// the ALU. |
`ifdef OPT_PIPELINED |
assign alu_stall = (((~master_ce)||(mem_rdbusy)||(alu_busy))&&(opvalid_alu)) //Case 1&2 |
// Old case #3--this isn't an ALU stall though ... |
||((opvalid_alu)&&(wr_reg_ce)&&(wr_reg_id[4] == op_gie) |
&&(wr_write_cc)) // Case 3 |
||((opvalid)&&(op_lock)&&(op_lock_stall)) |
||((opvalid)&&(op_break)) |
||(div_busy)||(fpu_busy); |
assign alu_ce = (master_ce)&&((opvalid_alu)||(op_illegal)) |
&&(~alu_stall) |
&&(~clear_pipeline); |
`else |
assign alu_stall = ((~master_ce)&&(opvalid_alu)) |
||((opvalid_alu)&&(op_break)); |
assign alu_ce = (master_ce)&&((opvalid_alu)||(op_illegal))&&(~alu_stall); |
`endif |
// |
|
// |
// Note: if you change the conditions for mem_ce, you must also change |
// alu_pc_valid. |
// |
`ifdef OPT_PIPELINED |
assign mem_ce = (master_ce)&&(opvalid_mem)&&(~mem_stalled) |
&&(~clear_pipeline); |
`else |
// If we aren't pipelined, then no one will be changing what's in the |
// pipeline (i.e. clear_pipeline), while our only instruction goes |
// through the ... pipeline. |
assign mem_ce = (master_ce)&&(opvalid_mem)&&(~mem_stalled); |
`endif |
`ifdef OPT_PIPELINED_BUS_ACCESS |
assign mem_stalled = (~master_ce)||(alu_busy)||((opvalid_mem)&&( |
(mem_pipe_stalled) |
||((~op_pipe)&&(mem_busy)) |
||(div_busy) |
||(fpu_busy) |
// Stall waiting for flags to be valid |
// Or waiting for a write to the PC register |
// Or CC register, since that can change the |
// PC as well |
||((wr_reg_ce)&&(wr_reg_id[4] == op_gie) |
&&((wr_write_pc)||(wr_write_cc))))); |
`else |
`ifdef OPT_PIPELINED |
assign mem_stalled = (mem_busy)||((opvalid_mem)&&( |
(~master_ce) |
// Stall waiting for flags to be valid |
// Or waiting for a write to the PC register |
// Or CC register, since that can change the |
// PC as well |
||((wr_reg_ce)&&(wr_reg_id[4] == op_gie)&&((wr_write_pc)||(wr_write_cc))))); |
`else |
assign mem_stalled = (opvalid_mem)&&(~master_ce); |
`endif |
`endif |
|
|
// |
// |
// PIPELINE STAGE #1 :: Prefetch |
// |
// |
`ifdef OPT_SINGLE_FETCH |
wire pf_ce; |
|
assign pf_ce = (~pf_valid)&&(~dcdvalid)&&(~opvalid)&&(~alu_valid); |
prefetch #(ADDRESS_WIDTH) |
pf(i_clk, i_rst, (pf_ce), (~dcd_stalled), pf_pc, gie, |
instruction, instruction_pc, instruction_gie, |
pf_valid, pf_illegal, |
pf_cyc, pf_stb, pf_we, pf_addr, pf_data, |
pf_ack, pf_stall, pf_err, i_wb_data); |
|
initial r_dcdvalid = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_dcdvalid <= 1'b0; |
else if (dcd_ce) |
r_dcdvalid <= (pf_valid); |
else if (op_ce) |
r_dcdvalid <= 1'b0; |
assign dcdvalid = r_dcdvalid; |
|
`else // Pipe fetch |
|
`ifdef OPT_TRADITIONAL_PFCACHE |
pfcache #(LGICACHE, ADDRESS_WIDTH) |
pf(i_clk, i_rst, (new_pc)||((dcd_early_branch)&&(~clear_pipeline)), |
i_clear_pf_cache, |
// dcd_pc, |
~dcd_stalled, |
((dcd_early_branch)&&(~clear_pipeline)) |
? dcd_branch_pc:pf_pc, |
instruction, instruction_pc, pf_valid, |
pf_cyc, pf_stb, pf_we, pf_addr, pf_data, |
pf_ack, pf_stall, pf_err, i_wb_data, |
pf_illegal); |
`else |
pipefetch #(RESET_ADDRESS, LGICACHE, ADDRESS_WIDTH) |
pf(i_clk, i_rst, (new_pc)||((dcd_early_branch)&&(~clear_pipeline)), |
i_clear_pf_cache, ~dcd_stalled, |
(new_pc)?pf_pc:dcd_branch_pc, |
instruction, instruction_pc, pf_valid, |
pf_cyc, pf_stb, pf_we, pf_addr, pf_data, |
pf_ack, pf_stall, pf_err, i_wb_data, |
//`ifdef OPT_PRECLEAR_BUS |
//((dcd_clear_bus)&&(dcdvalid)) |
//||((op_clear_bus)&&(opvalid)) |
//|| |
//`endif |
(mem_cyc_lcl)||(mem_cyc_gbl), |
pf_illegal); |
`endif |
assign instruction_gie = gie; |
|
initial r_dcdvalid = 1'b0; |
always @(posedge i_clk) |
if ((i_rst)||(clear_pipeline)) |
r_dcdvalid <= 1'b0; |
else if (dcd_ce) |
r_dcdvalid <= (pf_valid)&&(~clear_pipeline)&&(~dcd_ljmp)&&((~r_dcdvalid)||(~dcd_early_branch)); |
else if (op_ce) |
r_dcdvalid <= 1'b0; |
assign dcdvalid = r_dcdvalid; |
`endif |
|
`ifdef OPT_NEW_INSTRUCTION_SET |
idecode #(AW, IMPLEMENT_MPY, EARLY_BRANCHING, IMPLEMENT_DIVIDE, |
IMPLEMENT_FPU) |
instruction_decoder(i_clk, (i_rst)||(clear_pipeline), |
dcd_ce, dcd_stalled, instruction, instruction_gie, |
instruction_pc, pf_valid, pf_illegal, dcd_phase, |
dcd_illegal, dcd_pc, dcd_gie, |
{ dcdR_cc, dcdR_pc, dcdR }, |
{ dcdA_cc, dcdA_pc, dcdA }, |
{ dcdB_cc, dcdB_pc, dcdB }, |
dcdI, dcd_zI, dcdF, dcdF_wr, dcdOp, |
dcdALU, dcdM, dcdDV, dcdFP, dcd_break, dcd_lock, |
dcdR_wr,dcdA_rd, dcdB_rd, |
dcd_early_branch, |
dcd_branch_pc, dcd_ljmp, |
dcd_pipe); |
`else |
idecode_deprecated |
#(AW, IMPLEMENT_MPY, EARLY_BRANCHING, IMPLEMENT_DIVIDE, |
IMPLEMENT_FPU) |
instruction_decoder(i_clk, (i_rst)||(clear_pipeline), |
dcd_ce, dcd_stalled, instruction, instruction_gie, |
instruction_pc, pf_valid, pf_illegal, dcd_phase, |
dcd_illegal, dcd_pc, dcd_gie, |
{ dcdR_cc, dcdR_pc, dcdR }, |
{ dcdA_cc, dcdA_pc, dcdA }, |
{ dcdB_cc, dcdB_pc, dcdB }, |
dcdI, dcd_zI, dcdF, dcdF_wr, dcdOp, |
dcdALU, dcdM, dcdDV, dcdFP, dcd_break, dcd_lock, |
dcdR_wr,dcdA_rd, dcdB_rd, |
dcd_early_branch, |
dcd_branch_pc, |
dcd_pipe); |
assign dcd_ljmp = 1'b0; |
`endif |
|
`ifdef OPT_PIPELINED_BUS_ACCESS |
reg op_pipe; |
|
initial op_pipe = 1'b0; |
// To be a pipeable operation, there must be |
// two valid adjacent instructions |
// Both must be memory instructions |
// Both must be writes, or both must be reads |
// Both operations must be to the same identical address, |
// or at least a single (one) increment above that address |
// |
// However ... we need to know this before this clock, hence this is |
// calculated in the instruction decoder. |
always @(posedge i_clk) |
if (op_ce) |
op_pipe <= dcd_pipe; |
`endif |
|
// |
// |
// PIPELINE STAGE #3 :: Read Operands (Registers) |
// |
// |
assign w_opA = regset[dcdA]; |
assign w_opB = regset[dcdB]; |
|
wire [31:0] w_pcA_v; |
generate |
if (AW < 32) |
assign w_pcA_v = {{(32-AW){1'b0}}, (dcdA[4] == dcd_gie)?dcd_pc:upc }; |
else |
assign w_pcA_v = (dcdA[4] == dcd_gie)?dcd_pc:upc; |
endgenerate |
|
`ifdef OPT_PIPELINED |
reg [4:0] opA_id, opB_id; |
reg opA_rd, opB_rd; |
always @(posedge i_clk) |
if (op_ce) |
begin |
opA_id <= dcdA; |
opB_id <= dcdB; |
opA_rd <= dcdA_rd; |
opB_rd <= dcdB_rd; |
end |
`endif |
|
always @(posedge i_clk) |
if (op_ce) // &&(dcdvalid)) |
begin |
if ((wr_reg_ce)&&(wr_reg_id == dcdA)) |
r_opA <= wr_reg_vl; |
else if (dcdA_pc) |
r_opA <= w_pcA_v; |
else if (dcdA_cc) |
r_opA <= { w_opA[31:14], (dcdA[4])?w_uflags:w_iflags }; |
else |
r_opA <= w_opA; |
`ifdef OPT_PIPELINED |
end else |
begin // We were going to pick these up when they became valid, |
// but for some reason we're stuck here as they became |
// valid. Pick them up now anyway |
// if (((opA_alu)&&(alu_wr))||((opA_mem)&&(mem_valid))) |
// r_opA <= wr_reg_vl; |
if ((wr_reg_ce)&&(wr_reg_id == opA_id)&&(opA_rd)) |
r_opA <= wr_reg_vl; |
`endif |
end |
|
wire [31:0] w_opBnI, w_pcB_v; |
generate |
if (AW < 32) |
assign w_pcB_v = {{(32-AW){1'b0}}, (dcdB[4] == dcd_gie)?dcd_pc:upc }; |
else |
assign w_pcB_v = (dcdB[4] == dcd_gie)?dcd_pc:upc; |
endgenerate |
|
assign w_opBnI = (~dcdB_rd) ? 32'h00 |
: (((wr_reg_ce)&&(wr_reg_id == dcdB)) ? wr_reg_vl |
: ((dcdB_pc) ? w_pcB_v |
: ((dcdB_cc) ? { w_opB[31:14], (dcdB[4])?w_uflags:w_iflags} |
: w_opB))); |
|
always @(posedge i_clk) |
if (op_ce) // &&(dcdvalid)) |
r_opB <= w_opBnI + dcdI; |
`ifdef OPT_PIPELINED |
else if ((wr_reg_ce)&&(opB_id == wr_reg_id)&&(opB_rd)) |
r_opB <= wr_reg_vl; |
`endif |
|
// The logic here has become more complex than it should be, no thanks |
// to Xilinx's Vivado trying to help. The conditions are supposed to |
// be two sets of four bits: the top bits specify what bits matter, the |
// bottom specify what those top bits must equal. However, two of |
// conditions check whether bits are on, and those are the only two |
// conditions checking those bits. Therefore, Vivado complains that |
// these two bits are redundant. Hence the convoluted expression |
// below, arriving at what we finally want in the (now wire net) |
// opF. |
always @(posedge i_clk) |
if (op_ce) |
begin // Set the flag condition codes, bit order is [3:0]=VNCZ |
case(dcdF[2:0]) |
3'h0: r_opF <= 6'h00; // Always |
`ifdef OPT_NEW_INSTRUCTION_SET |
// These were remapped as part of the new instruction |
// set in order to make certain that the low order |
// two bits contained the most commonly used |
// conditions: Always, LT, Z, and NZ. |
3'h1: r_opF <= 6'h24; // LT |
3'h2: r_opF <= 6'h11; // Z |
3'h3: r_opF <= 6'h10; // NE |
3'h4: r_opF <= 6'h30; // GT (!N&!Z) |
3'h5: r_opF <= 6'h20; // GE (!N) |
`else |
3'h1: r_opF <= 6'h11; // Z |
3'h2: r_opF <= 6'h10; // NE |
3'h3: r_opF <= 6'h20; // GE (!N) |
3'h4: r_opF <= 6'h30; // GT (!N&!Z) |
3'h5: r_opF <= 6'h24; // LT |
`endif |
3'h6: r_opF <= 6'h02; // C |
3'h7: r_opF <= 6'h08; // V |
endcase |
end // Bit order is { (flags_not_used), VNCZ mask, VNCZ value } |
assign opF = { r_opF[3], r_opF[5], r_opF[1], r_opF[4:0] }; |
|
wire w_opvalid; |
assign w_opvalid = (~clear_pipeline)&&(dcdvalid)&&(~dcd_ljmp); |
initial opvalid = 1'b0; |
initial opvalid_alu = 1'b0; |
initial opvalid_mem = 1'b0; |
initial opvalid_div = 1'b0; |
initial opvalid_fpu = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
begin |
opvalid <= 1'b0; |
opvalid_alu <= 1'b0; |
opvalid_mem <= 1'b0; |
end else if (op_ce) |
begin |
// Do we have a valid instruction? |
// The decoder may vote to stall one of its |
// instructions based upon something we currently |
// have in our queue. This instruction must then |
// move forward, and get a stall cycle inserted. |
// Hence, the test on dcd_stalled here. If we must |
// wait until our operands are valid, then we aren't |
// valid yet until then. |
opvalid<= w_opvalid; |
`ifdef OPT_ILLEGAL_INSTRUCTION |
opvalid_alu <= ((dcdALU)||(dcd_illegal))&&(w_opvalid); |
opvalid_mem <= (dcdM)&&(~dcd_illegal)&&(w_opvalid); |
opvalid_div <= (dcdDV)&&(~dcd_illegal)&&(w_opvalid); |
opvalid_fpu <= (dcdFP)&&(~dcd_illegal)&&(w_opvalid); |
`else |
opvalid_alu <= (dcdALU)&&(w_opvalid); |
opvalid_mem <= (dcdM)&&(w_opvalid); |
opvalid_div <= (dcdDV)&&(w_opvalid); |
opvalid_fpu <= (dcdFP)&&(w_opvalid); |
`endif |
end else if ((clear_pipeline)||(alu_ce)||(mem_ce)||(div_ce)||(fpu_ce)) |
begin |
opvalid <= 1'b0; |
opvalid_alu <= 1'b0; |
opvalid_mem <= 1'b0; |
opvalid_div <= 1'b0; |
opvalid_fpu <= 1'b0; |
end |
|
// Here's part of our debug interface. When we recognize a break |
// instruction, we set the op_break flag. That'll prevent this |
// instruction from entering the ALU, and cause an interrupt before |
// this instruction. Thus, returning to this code will cause the |
// break to repeat and continue upon return. To get out of this |
// condition, replace the break instruction with what it is supposed |
// to be, step through it, and then replace it back. In this fashion, |
// a debugger can step through code. |
// assign w_op_break = (dcd_break)&&(r_dcdI[15:0] == 16'h0001); |
initial op_break = 1'b0; |
always @(posedge i_clk) |
if (i_rst) op_break <= 1'b0; |
else if (op_ce) op_break <= (dcd_break); |
else if ((clear_pipeline)||(~opvalid)) |
op_break <= 1'b0; |
|
`ifdef OPT_PIPELINED |
generate |
if (IMPLEMENT_LOCK != 0) |
begin |
reg r_op_lock, r_op_lock_stall; |
|
initial r_op_lock_stall = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_op_lock_stall <= 1'b0; |
else |
r_op_lock_stall <= (~opvalid)||(~op_lock) |
||(~dcdvalid)||(~pf_valid); |
|
assign op_lock_stall = r_op_lock_stall; |
|
initial r_op_lock = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_op_lock <= 1'b0; |
else if ((op_ce)&&(dcd_lock)) |
r_op_lock <= 1'b1; |
else if ((op_ce)||(clear_pipeline)) |
r_op_lock <= 1'b0; |
assign op_lock = r_op_lock; |
|
end else begin |
assign op_lock_stall = 1'b0; |
assign op_lock = 1'b0; |
end endgenerate |
|
`else |
assign op_lock_stall = 1'b0; |
assign op_lock = 1'b0; |
`endif |
|
`ifdef OPT_ILLEGAL_INSTRUCTION |
initial op_illegal = 1'b0; |
always @(posedge i_clk) |
if ((i_rst)||(clear_pipeline)) |
op_illegal <= 1'b0; |
else if(op_ce) |
`ifdef OPT_PIPELINED |
op_illegal <=(dcd_illegal)||((dcd_lock)&&(IMPLEMENT_LOCK == 0)); |
`else |
op_illegal <= (dcd_illegal)||(dcd_lock); |
`endif |
`endif |
|
// No generate on EARLY_BRANCHING here, since if EARLY_BRANCHING is not |
// set, dcd_early_branch will simply be a wire connected to zero and |
// this logic should just optimize. |
always @(posedge i_clk) |
if (op_ce) |
begin |
opF_wr <= (dcdF_wr)&&((~dcdR_cc)||(~dcdR_wr)) |
&&(~dcd_early_branch)&&(~dcd_illegal); |
opR_wr <= (dcdR_wr)&&(~dcd_early_branch)&&(~dcd_illegal); |
end |
|
always @(posedge i_clk) |
if (op_ce) |
begin |
opn <= dcdOp; // Which ALU operation? |
// opM <= dcdM; // Is this a memory operation? |
// What register will these results be written into? |
opR <= dcdR; |
opR_cc <= (dcdR_cc)&&(dcdR_wr)&&(dcdR[4]==dcd_gie); |
// User level (1), vs supervisor (0)/interrupts disabled |
op_gie <= dcd_gie; |
|
|
// |
op_pc <= (dcd_early_branch)?dcd_branch_pc:dcd_pc; |
end |
assign opFl = (op_gie)?(w_uflags):(w_iflags); |
|
`ifdef OPT_VLIW |
reg r_op_phase; |
initial r_op_phase = 1'b0; |
always @(posedge i_clk) |
if ((i_rst)||(clear_pipeline)) |
r_op_phase <= 1'b0; |
else if (op_ce) |
r_op_phase <= dcd_phase; |
assign op_phase = r_op_phase; |
`else |
assign op_phase = 1'b0; |
`endif |
|
// This is tricky. First, the PC and Flags registers aren't kept in |
// register set but in special registers of their own. So step one |
// is to select the right register. Step to is to replace that |
// register with the results of an ALU or memory operation, if such |
// results are now available. Otherwise, we'd need to insert a wait |
// state of some type. |
// |
// The alternative approach would be to define some sort of |
// op_stall wire, which would stall any upstream stage. |
// We'll create a flag here to start our coordination. Once we |
// define this flag to something other than just plain zero, then |
// the stalls will already be in place. |
`ifdef OPT_PIPELINED |
assign opA = ((wr_reg_ce)&&(wr_reg_id == opA_id)) // &&(opA_rd)) |
? wr_reg_vl : r_opA; |
`else |
assign opA = r_opA; |
`endif |
|
`ifdef OPT_PIPELINED |
// Stall if we have decoded an instruction that will read register A |
// AND ... something that may write a register is running |
// AND (series of conditions here ...) |
// The operation might set flags, and we wish to read the |
// CC register |
// OR ... (No other conditions) |
assign dcdA_stall = (dcdA_rd) // &&(dcdvalid) is checked for elsewhere |
&&((opvalid)||(mem_rdbusy) |
||(div_busy)||(fpu_busy)) |
&&((opF_wr)&&(dcdA_cc)); |
`else |
// There are no pipeline hazards, if we aren't pipelined |
assign dcdA_stall = 1'b0; |
`endif |
|
`ifdef OPT_PIPELINED |
assign opB = ((wr_reg_ce)&&(wr_reg_id == opB_id)&&(opB_rd)) |
? wr_reg_vl: r_opB; |
`else |
assign opB = r_opB; |
`endif |
|
`ifdef OPT_PIPELINED |
// Stall if we have decoded an instruction that will read register B |
// AND ... something that may write a (unknown) register is running |
// AND (series of conditions here ...) |
// The operation might set flags, and we wish to read the |
// CC register |
// OR the operation might set register B, and we still need |
// a clock to add the offset to it |
assign dcdB_stall = (dcdB_rd) // &&(dcdvalid) is checked for elsewhere |
// If the op stage isn't valid, yet something |
// is running, then it must have been valid. |
// We'll use the last values from that stage |
// (opR_wr, opF_wr, opR) in our logic below. |
&&((opvalid)||(mem_rdbusy) |
||(div_busy)||(fpu_busy)) |
&&( |
// Stall on memory ops writing to my register |
// (i.e. loads), or on any write to my |
// register if I have an immediate offset |
// Note the exception for writing to the PC: |
// if I write to the PC, the whole next |
// instruction is invalid, not just the |
// operand. That'll get wiped in the |
// next operation anyway, so don't stall |
// here. This keeps a BC X, BNZ Y from |
// stalling between the two branches. |
// BC X, BRA Y is still clear, since BRA Y |
// is an early branch instruction. |
// (This exception is commented out in |
// order to help keep our logic simple, and |
// because multiple conditional branches |
// following each other constitutes a |
// fairly unusualy code structure.) |
// |
((~dcd_zI)&&(opR == dcdB)&&(opR_wr)) |
// &&(opR != { op_gie, `CPU_PC_REG } ) |
// Stall following any instruction that will |
// set the flags, if we're going to need the |
// flags (CC) register for opB. |
||((opF_wr)&&(dcdB_cc)) |
// Stall on any ongoing memory operation that |
// will write to opB -- captured above |
// ||((mem_busy)&&(~mem_we)&&(mem_last_reg==dcdB)&&(~dcd_zI)) |
); |
assign dcdF_stall = ((~dcdF[3]) |
||((dcdA_rd)&&(dcdA_cc)) |
||((dcdB_rd)&&(dcdB_cc))) |
&&(opvalid)&&(opR_cc); |
// &&(dcdvalid) is checked for elsewhere |
`else |
// No stalls without pipelining, 'cause how can you have a pipeline |
// hazard without the pipeline? |
assign dcdB_stall = 1'b0; |
assign dcdF_stall = 1'b0; |
`endif |
// |
// |
// PIPELINE STAGE #4 :: Apply Instruction |
// |
// |
`ifdef OPT_NEW_INSTRUCTION_SET |
cpuops #(IMPLEMENT_MPY) doalu(i_clk, i_rst, alu_ce, |
(opvalid_alu), opn, opA, opB, |
alu_result, alu_flags, alu_valid, alu_illegal_op, |
alu_busy); |
`else |
cpuops_deprecated #(IMPLEMENT_MPY) doalu(i_clk, i_rst, alu_ce, |
(opvalid_alu), opn, opA, opB, |
alu_result, alu_flags, alu_valid, alu_illegal_op); |
assign alu_busy = 1'b0; |
`endif |
|
generate |
if (IMPLEMENT_DIVIDE != 0) |
begin |
div thedivide(i_clk, (i_rst)||(clear_pipeline), div_ce, opn[0], |
opA, opB, div_busy, div_valid, div_error, div_result, |
div_flags); |
end else begin |
assign div_error = 1'b1; |
assign div_busy = 1'b0; |
assign div_valid = 1'b0; |
assign div_result= 32'h00; |
assign div_flags = 4'h0; |
end endgenerate |
|
generate |
if (IMPLEMENT_FPU != 0) |
begin |
// |
// sfpu thefpu(i_clk, i_rst, fpu_ce, |
// opA, opB, fpu_busy, fpu_valid, fpu_err, fpu_result, |
// fpu_flags); |
// |
assign fpu_error = 1'b1; |
assign fpu_busy = 1'b0; |
assign fpu_valid = 1'b0; |
assign fpu_result= 32'h00; |
assign fpu_flags = 4'h0; |
end else begin |
assign fpu_error = 1'b1; |
assign fpu_busy = 1'b0; |
assign fpu_valid = 1'b0; |
assign fpu_result= 32'h00; |
assign fpu_flags = 4'h0; |
end endgenerate |
|
|
assign set_cond = ((opF[7:4]&opFl[3:0])==opF[3:0]); |
initial alF_wr = 1'b0; |
initial alu_wr = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
begin |
alu_wr <= 1'b0; |
alF_wr <= 1'b0; |
end else if (alu_ce) |
begin |
// alu_reg <= opR; |
alu_wr <= (opR_wr)&&(set_cond); |
alF_wr <= (opF_wr)&&(set_cond); |
end else if (~alu_busy) begin |
// These are strobe signals, so clear them if not |
// set for any particular clock |
alu_wr <= (i_halt)&&(i_dbg_we); |
alF_wr <= 1'b0; |
end |
|
`ifdef OPT_VLIW |
reg r_alu_phase; |
initial r_alu_phase = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_alu_phase <= 1'b0; |
else if ((alu_ce)||(mem_ce)||(div_ce)||(fpu_ce)) |
r_alu_phase <= op_phase; |
assign alu_phase = r_alu_phase; |
`else |
assign alu_phase = 1'b0; |
`endif |
|
always @(posedge i_clk) |
if ((alu_ce)||(div_ce)||(fpu_ce)) |
alu_reg <= opR; |
else if ((i_halt)&&(i_dbg_we)) |
alu_reg <= i_dbg_reg; |
|
reg [31:0] dbg_val; |
reg dbgv; |
always @(posedge i_clk) |
dbg_val <= i_dbg_data; |
initial dbgv = 1'b0; |
always @(posedge i_clk) |
dbgv <= (~i_rst)&&(~alu_ce)&&((i_halt)&&(i_dbg_we)); |
always @(posedge i_clk) |
if ((alu_ce)||(mem_ce)) |
alu_gie <= op_gie; |
always @(posedge i_clk) |
if ((alu_ce)||((master_ce)&&(opvalid_mem)&&(~clear_pipeline) |
&&(~mem_stalled))) |
alu_pc <= op_pc; |
|
`ifdef OPT_ILLEGAL_INSTRUCTION |
reg r_alu_illegal; |
initial r_alu_illegal = 0; |
always @(posedge i_clk) |
if (clear_pipeline) |
r_alu_illegal <= 1'b0; |
else if ((alu_ce)||(mem_ce)) |
r_alu_illegal <= op_illegal; |
assign alu_illegal = (alu_illegal_op)||(r_alu_illegal); |
`endif |
|
// This _almost_ is equal to (alu_ce)||(mem_ce). The only |
// problem is that mem_ce is gated by the set_cond, and |
// the PC will be valid independent of the set condition. Hence, this |
// equals (alu_ce)||(everything in mem_ce but the set condition) |
initial alu_pc_valid = 1'b0; |
always @(posedge i_clk) |
alu_pc_valid <= ((alu_ce) |
||((master_ce)&&(opvalid_mem)&&(~clear_pipeline)&&(~mem_stalled))); |
|
wire bus_lock; |
`ifdef OPT_PIPELINED |
generate |
if (IMPLEMENT_LOCK != 0) |
begin |
reg r_bus_lock; |
initial r_bus_lock = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_bus_lock <= 1'b0; |
else if ((op_ce)&&(op_lock)) |
r_bus_lock <= 1'b1; |
else if (~opvalid_mem) |
r_bus_lock <= 1'b0; |
assign bus_lock = r_bus_lock; |
end else begin |
assign bus_lock = 1'b0; |
end endgenerate |
`else |
assign bus_lock = 1'b0; |
`endif |
|
`ifdef OPT_PIPELINED_BUS_ACCESS |
pipemem #(AW,IMPLEMENT_LOCK) domem(i_clk, i_rst,(mem_ce)&&(set_cond), bus_lock, |
(opn[0]), opB, opA, opR, |
mem_busy, mem_pipe_stalled, |
mem_valid, bus_err, mem_wreg, mem_result, |
mem_cyc_gbl, mem_cyc_lcl, |
mem_stb_gbl, mem_stb_lcl, |
mem_we, mem_addr, mem_data, |
mem_ack, mem_stall, mem_err, i_wb_data); |
|
`else // PIPELINED_BUS_ACCESS |
memops #(AW,IMPLEMENT_LOCK) domem(i_clk, i_rst,(mem_ce)&&(set_cond), bus_lock, |
(opn[0]), opB, opA, opR, |
mem_busy, |
mem_valid, bus_err, mem_wreg, mem_result, |
mem_cyc_gbl, mem_cyc_lcl, |
mem_stb_gbl, mem_stb_lcl, |
mem_we, mem_addr, mem_data, |
mem_ack, mem_stall, mem_err, i_wb_data); |
`endif // PIPELINED_BUS_ACCESS |
assign mem_rdbusy = ((mem_busy)&&(~mem_we)); |
|
// Either the prefetch or the instruction gets the memory bus, but |
// never both. |
wbdblpriarb #(32,AW) pformem(i_clk, i_rst, |
// Memory access to the arbiter, priority position |
mem_cyc_gbl, mem_cyc_lcl, mem_stb_gbl, mem_stb_lcl, |
mem_we, mem_addr, mem_data, mem_ack, mem_stall, mem_err, |
// Prefetch access to the arbiter |
pf_cyc, 1'b0, pf_stb, 1'b0, pf_we, pf_addr, pf_data, |
pf_ack, pf_stall, pf_err, |
// Common wires, in and out, of the arbiter |
o_wb_gbl_cyc, o_wb_lcl_cyc, o_wb_gbl_stb, o_wb_lcl_stb, |
o_wb_we, o_wb_addr, o_wb_data, |
i_wb_ack, i_wb_stall, i_wb_err); |
|
// |
// |
// PIPELINE STAGE #5 :: Write-back results |
// |
// |
// This stage is not allowed to stall. If results are ready to be |
// written back, they are written back at all cost. Sleepy CPU's |
// won't prevent write back, nor debug modes, halting the CPU, nor |
// anything else. Indeed, the (master_ce) bit is only as relevant |
// as knowinig something is available for writeback. |
|
// |
// Write back to our generic register set ... |
// When shall we write back? On one of two conditions |
// Note that the flags needed to be checked before issuing the |
// bus instruction, so they don't need to be checked here. |
// Further, alu_wr includes (set_cond), so we don't need to |
// check for that here either. |
`ifdef OPT_ILLEGAL_INSTRUCTION |
assign wr_reg_ce = (~alu_illegal)&& |
(((alu_wr)&&(~clear_pipeline) |
&&((alu_valid)||(div_valid)||(fpu_valid))) |
||(mem_valid)); |
`else |
assign wr_reg_ce = ((alu_wr)&&(~clear_pipeline))||(mem_valid)||(div_valid)||(fpu_valid); |
`endif |
// Which register shall be written? |
// COULD SIMPLIFY THIS: by adding three bits to these registers, |
// One or PC, one for CC, and one for GIE match |
// Note that the alu_reg is the register to write on a divide or |
// FPU operation. |
assign wr_reg_id = (alu_wr)?alu_reg:mem_wreg; |
// Are we writing to the CC register? |
assign wr_write_cc = (wr_reg_id[3:0] == `CPU_CC_REG); |
// Are we writing to the PC? |
assign wr_write_pc = (wr_reg_id[3:0] == `CPU_PC_REG); |
// What value to write? |
assign wr_reg_vl = ((mem_valid) ? mem_result |
:((div_valid|fpu_valid)) |
? ((div_valid) ? div_result:fpu_result) |
:((dbgv) ? dbg_val : alu_result)); |
always @(posedge i_clk) |
if (wr_reg_ce) |
regset[wr_reg_id] <= wr_reg_vl; |
|
// |
// Write back to the condition codes/flags register ... |
// When shall we write to our flags register? alF_wr already |
// includes the set condition ... |
assign wr_flags_ce = ((alF_wr)||(div_valid)||(fpu_valid))&&(~clear_pipeline)&&(~alu_illegal); |
assign w_uflags = { uhalt_phase, ufpu_err_flag, |
udiv_err_flag, ubus_err_flag, trap, ill_err_u, |
1'b0, step, 1'b1, sleep, |
((wr_flags_ce)&&(alu_gie))?alu_flags:flags }; |
assign w_iflags = { ihalt_phase, ifpu_err_flag, |
idiv_err_flag, ibus_err_flag, trap, ill_err_i, |
break_en, 1'b0, 1'b0, sleep, |
((wr_flags_ce)&&(~alu_gie))?alu_flags:iflags }; |
|
|
// What value to write? |
always @(posedge i_clk) |
// If explicitly writing the register itself |
if ((wr_reg_ce)&&(wr_reg_id[4])&&(wr_write_cc)) |
flags <= wr_reg_vl[3:0]; |
// Otherwise if we're setting the flags from an ALU operation |
else if ((wr_flags_ce)&&(alu_gie)) |
flags <= (div_valid)?div_flags:((fpu_valid)?fpu_flags |
: alu_flags); |
|
always @(posedge i_clk) |
if ((wr_reg_ce)&&(~wr_reg_id[4])&&(wr_write_cc)) |
iflags <= wr_reg_vl[3:0]; |
else if ((wr_flags_ce)&&(~alu_gie)) |
iflags <= (div_valid)?div_flags:((fpu_valid)?fpu_flags |
: alu_flags); |
|
// The 'break' enable bit. This bit can only be set from supervisor |
// mode. It control what the CPU does upon encountering a break |
// instruction. |
// |
// The goal, upon encountering a break is that the CPU should stop and |
// not execute the break instruction, choosing instead to enter into |
// either interrupt mode or halt first. |
// if ((break_en) AND (break_instruction)) // user mode or not |
// HALT CPU |
// else if (break_instruction) // only in user mode |
// set an interrupt flag, go to supervisor mode |
// allow supervisor to step the CPU. |
// Upon a CPU halt, any break condition will be reset. The |
// external debugger will then need to deal with whatever |
// condition has taken place. |
initial break_en = 1'b0; |
always @(posedge i_clk) |
if ((i_rst)||(i_halt)) |
break_en <= 1'b0; |
else if ((wr_reg_ce)&&(~wr_reg_id[4])&&(wr_write_cc)) |
break_en <= wr_reg_vl[`CPU_BREAK_BIT]; |
`ifdef OPT_ILLEGAL_INSTRUCTION |
assign o_break = ((break_en)||(~op_gie))&&(op_break) |
&&(~alu_valid)&&(~mem_valid)&&(~mem_busy) |
&&(~div_busy)&&(~fpu_busy) |
&&(~clear_pipeline) |
||((~alu_gie)&&(bus_err)) |
||((~alu_gie)&&(div_valid)&&(div_error)) |
||((~alu_gie)&&(fpu_valid)&&(fpu_error)) |
||((~alu_gie)&&(alu_pc_valid)&&(alu_illegal)); |
`else |
assign o_break = (((break_en)||(~op_gie))&&(op_break) |
&&(~alu_valid)&&(~mem_valid)&&(~mem_busy) |
&&(~clear_pipeline)) |
||((~alu_gie)&&(bus_err)); |
`endif |
|
|
// The sleep register. Setting the sleep register causes the CPU to |
// sleep until the next interrupt. Setting the sleep register within |
// interrupt mode causes the processor to halt until a reset. This is |
// a panic/fault halt. The trick is that you cannot be allowed to |
// set the sleep bit and switch to supervisor mode in the same |
// instruction: users are not allowed to halt the CPU. |
always @(posedge i_clk) |
if ((i_rst)||(w_switch_to_interrupt)) |
sleep <= 1'b0; |
else if ((wr_reg_ce)&&(wr_write_cc)&&(~alu_gie)) |
// In supervisor mode, we have no protections. The |
// supervisor can set the sleep bit however he wants. |
// Well ... not quite. Switching to user mode and |
// sleep mode shouold only be possible if the interrupt |
// flag isn't set. |
// Thus: if (i_interrupt)&&(wr_reg_vl[GIE]) |
// don't set the sleep bit |
// otherwise however it would o.w. be set |
sleep <= (wr_reg_vl[`CPU_SLEEP_BIT]) |
&&((~i_interrupt)||(~wr_reg_vl[`CPU_GIE_BIT])); |
else if ((wr_reg_ce)&&(wr_write_cc)&&(wr_reg_vl[`CPU_GIE_BIT])) |
// In user mode, however, you can only set the sleep |
// mode while remaining in user mode. You can't switch |
// to sleep mode *and* supervisor mode at the same |
// time, lest you halt the CPU. |
sleep <= wr_reg_vl[`CPU_SLEEP_BIT]; |
|
always @(posedge i_clk) |
if ((i_rst)||(w_switch_to_interrupt)) |
step <= 1'b0; |
else if ((wr_reg_ce)&&(~alu_gie)&&(wr_reg_id[4])&&(wr_write_cc)) |
step <= wr_reg_vl[`CPU_STEP_BIT]; |
else if ((alu_pc_valid)&&(step)&&(gie)) |
step <= 1'b0; |
|
// The GIE register. Only interrupts can disable the interrupt register |
assign w_switch_to_interrupt = (gie)&&( |
// On interrupt (obviously) |
((i_interrupt)&&(~alu_phase)&&(~bus_lock)) |
// If we are stepping the CPU |
||((alu_pc_valid)&&(step)&&(~alu_phase)&&(~bus_lock)) |
// If we encounter a break instruction, if the break |
// enable isn't set. |
||((master_ce)&&(~mem_rdbusy)&&(~div_busy)&&(~fpu_busy) |
&&(op_break)&&(~break_en)) |
`ifdef OPT_ILLEGAL_INSTRUCTION |
// On an illegal instruction |
||((alu_pc_valid)&&(alu_illegal)) |
`endif |
// On division by zero. If the divide isn't |
// implemented, div_valid and div_error will be short |
// circuited and that logic will be bypassed |
||((div_valid)&&(div_error)) |
// Same thing on a floating point error. |
||((fpu_valid)&&(fpu_error)) |
// |
||(bus_err) |
// If we write to the CC register |
||((wr_reg_ce)&&(~wr_reg_vl[`CPU_GIE_BIT]) |
&&(wr_reg_id[4])&&(wr_write_cc)) |
); |
assign w_release_from_interrupt = (~gie)&&(~i_interrupt) |
// Then if we write the CC register |
&&(((wr_reg_ce)&&(wr_reg_vl[`CPU_GIE_BIT]) |
&&(~wr_reg_id[4])&&(wr_write_cc)) |
); |
always @(posedge i_clk) |
if (i_rst) |
gie <= 1'b0; |
else if (w_switch_to_interrupt) |
gie <= 1'b0; |
else if (w_release_from_interrupt) |
gie <= 1'b1; |
|
initial trap = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
trap <= 1'b0; |
else if ((alu_gie)&&(wr_reg_ce)&&(~wr_reg_vl[`CPU_GIE_BIT]) |
&&(wr_write_cc)) // &&(wr_reg_id[4]) implied |
trap <= 1'b1; |
else if (w_release_from_interrupt) |
trap <= 1'b0; |
|
`ifdef OPT_ILLEGAL_INSTRUCTION |
initial ill_err_i = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
ill_err_i <= 1'b0; |
// The debug interface can clear this bit |
else if ((dbgv)&&(wr_reg_id == {1'b0, `CPU_CC_REG}) |
&&(~wr_reg_vl[`CPU_ILL_BIT])) |
ill_err_i <= 1'b0; |
else if ((alu_pc_valid)&&(alu_illegal)&&(~alu_gie)) |
ill_err_i <= 1'b1; |
initial ill_err_u = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
ill_err_u <= 1'b0; |
// The bit is automatically cleared on release from interrupt |
else if (w_release_from_interrupt) |
ill_err_u <= 1'b0; |
// If the supervisor writes to this register, clearing the |
// bit, then clear it |
else if (((~alu_gie)||(dbgv)) |
&&(wr_reg_ce)&&(~wr_reg_vl[`CPU_ILL_BIT]) |
&&(wr_reg_id[4])&&(wr_write_cc)) |
ill_err_u <= 1'b0; |
else if ((alu_pc_valid)&&(alu_illegal)&&(alu_gie)) |
ill_err_u <= 1'b1; |
`else |
assign ill_err_u = 1'b0; |
assign ill_err_i = 1'b0; |
`endif |
// Supervisor/interrupt bus error flag -- this will crash the CPU if |
// ever set. |
initial ibus_err_flag = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
ibus_err_flag <= 1'b0; |
else if ((dbgv)&&(wr_reg_id == {1'b0, `CPU_CC_REG}) |
&&(~wr_reg_vl[`CPU_BUSERR_BIT])) |
ibus_err_flag <= 1'b0; |
else if ((bus_err)&&(~alu_gie)) |
ibus_err_flag <= 1'b1; |
// User bus error flag -- if ever set, it will cause an interrupt to |
// supervisor mode. |
initial ubus_err_flag = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
ubus_err_flag <= 1'b0; |
else if (w_release_from_interrupt) |
ubus_err_flag <= 1'b0; |
// else if ((i_halt)&&(i_dbg_we)&&(~i_dbg_reg[4]) |
// &&(i_dbg_reg == {1'b1, `CPU_CC_REG}) |
// &&(~i_dbg_data[`CPU_BUSERR_BIT])) |
// ubus_err_flag <= 1'b0; |
else if (((~alu_gie)||(dbgv))&&(wr_reg_ce) |
&&(~wr_reg_vl[`CPU_BUSERR_BIT]) |
&&(wr_reg_id[4])&&(wr_write_cc)) |
ubus_err_flag <= 1'b0; |
else if ((bus_err)&&(alu_gie)) |
ubus_err_flag <= 1'b1; |
|
generate |
if (IMPLEMENT_DIVIDE != 0) |
begin |
reg r_idiv_err_flag, r_udiv_err_flag; |
|
// Supervisor/interrupt divide (by zero) error flag -- this will |
// crash the CPU if ever set. This bit is thus available for us |
// to be able to tell if/why the CPU crashed. |
initial r_idiv_err_flag = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_idiv_err_flag <= 1'b0; |
else if ((dbgv)&&(wr_reg_id == {1'b0, `CPU_CC_REG}) |
&&(~wr_reg_vl[`CPU_DIVERR_BIT])) |
r_idiv_err_flag <= 1'b0; |
else if ((div_error)&&(div_valid)&&(~alu_gie)) |
r_idiv_err_flag <= 1'b1; |
// User divide (by zero) error flag -- if ever set, it will |
// cause a sudden switch interrupt to supervisor mode. |
initial r_udiv_err_flag = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_udiv_err_flag <= 1'b0; |
else if (w_release_from_interrupt) |
r_udiv_err_flag <= 1'b0; |
else if (((~alu_gie)||(dbgv))&&(wr_reg_ce) |
&&(~wr_reg_vl[`CPU_DIVERR_BIT]) |
&&(wr_reg_id[4])&&(wr_write_cc)) |
r_udiv_err_flag <= 1'b0; |
else if ((div_error)&&(alu_gie)&&(div_valid)) |
r_udiv_err_flag <= 1'b1; |
|
assign idiv_err_flag = r_idiv_err_flag; |
assign udiv_err_flag = r_udiv_err_flag; |
end else begin |
assign idiv_err_flag = 1'b0; |
assign udiv_err_flag = 1'b0; |
end endgenerate |
|
generate |
if (IMPLEMENT_FPU !=0) |
begin |
// Supervisor/interrupt floating point error flag -- this will |
// crash the CPU if ever set. |
reg r_ifpu_err_flag, r_ufpu_err_flag; |
initial r_ifpu_err_flag = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_ifpu_err_flag <= 1'b0; |
else if ((dbgv)&&(wr_reg_id == {1'b0, `CPU_CC_REG}) |
&&(~wr_reg_vl[`CPU_FPUERR_BIT])) |
r_ifpu_err_flag <= 1'b0; |
else if ((fpu_error)&&(fpu_valid)&&(~alu_gie)) |
r_ifpu_err_flag <= 1'b1; |
// User floating point error flag -- if ever set, it will cause |
// a sudden switch interrupt to supervisor mode. |
initial r_ufpu_err_flag = 1'b0; |
always @(posedge i_clk) |
if (i_rst) |
r_ufpu_err_flag <= 1'b0; |
else if (w_release_from_interrupt) |
r_ufpu_err_flag <= 1'b0; |
else if (((~alu_gie)||(dbgv))&&(wr_reg_ce) |
&&(~wr_reg_vl[`CPU_FPUERR_BIT]) |
&&(wr_reg_id[4])&&(wr_write_cc)) |
r_ufpu_err_flag <= 1'b0; |
else if ((fpu_error)&&(alu_gie)&&(fpu_valid)) |
r_ufpu_err_flag <= 1'b1; |
|
assign ifpu_err_flag = r_ifpu_err_flag; |
assign ufpu_err_flag = r_ufpu_err_flag; |
end else begin |
assign ifpu_err_flag = 1'b0; |
assign ufpu_err_flag = 1'b0; |
end endgenerate |
|
`ifdef OPT_VLIW |
reg r_ihalt_phase, r_uhalt_phase; |
|
initial r_ihalt_phase = 0; |
initial r_uhalt_phase = 0; |
always @(posedge i_clk) |
if (~alu_gie) |
r_ihalt_phase <= alu_phase; |
always @(posedge i_clk) |
if (alu_gie) |
r_uhalt_phase <= alu_phase; |
else if (w_release_from_interrupt) |
r_uhalt_phase <= 1'b0; |
|
assign ihalt_phase = r_ihalt_phase; |
assign uhalt_phase = r_uhalt_phase; |
`else |
assign ihalt_phase = 1'b0; |
assign uhalt_phase = 1'b0; |
`endif |
|
// |
// Write backs to the PC register, and general increments of it |
// We support two: upc and ipc. If the instruction is normal, |
// we increment upc, if interrupt level we increment ipc. If |
// the instruction writes the PC, we write whichever PC is appropriate. |
// |
// Do we need to all our partial results from the pipeline? |
// What happens when the pipeline has gie and ~gie instructions within |
// it? Do we clear both? What if a gie instruction tries to clear |
// a non-gie instruction? |
always @(posedge i_clk) |
if ((wr_reg_ce)&&(wr_reg_id[4])&&(wr_write_pc)) |
upc <= wr_reg_vl[(AW-1):0]; |
else if ((alu_gie)&&(alu_pc_valid)&&(~clear_pipeline)) |
upc <= alu_pc; |
|
always @(posedge i_clk) |
if (i_rst) |
ipc <= RESET_ADDRESS; |
else if ((wr_reg_ce)&&(~wr_reg_id[4])&&(wr_write_pc)) |
ipc <= wr_reg_vl[(AW-1):0]; |
else if ((~alu_gie)&&(alu_pc_valid)&&(~clear_pipeline)) |
ipc <= alu_pc; |
|
always @(posedge i_clk) |
if (i_rst) |
pf_pc <= RESET_ADDRESS; |
else if (w_switch_to_interrupt) |
pf_pc <= ipc; |
else if (w_release_from_interrupt) |
pf_pc <= upc; |
else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc)) |
pf_pc <= wr_reg_vl[(AW-1):0]; |
`ifdef OPT_PIPELINED |
else if ((dcd_early_branch)&&(~clear_pipeline)) |
pf_pc <= dcd_branch_pc + 1; |
else if ((new_pc)||((~dcd_stalled)&&(pf_valid))) |
pf_pc <= pf_pc + {{(AW-1){1'b0}},1'b1}; |
`else |
else if ((alu_pc_valid)&&(~clear_pipeline)) |
pf_pc <= alu_pc; |
`endif |
|
initial new_pc = 1'b1; |
always @(posedge i_clk) |
if ((i_rst)||(i_clear_pf_cache)) |
new_pc <= 1'b1; |
else if (w_switch_to_interrupt) |
new_pc <= 1'b1; |
else if (w_release_from_interrupt) |
new_pc <= 1'b1; |
else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc)) |
new_pc <= 1'b1; |
else |
new_pc <= 1'b0; |
|
// |
// The debug interface |
generate |
if (AW<32) |
begin |
always @(posedge i_clk) |
begin |
o_dbg_reg <= regset[i_dbg_reg]; |
if (i_dbg_reg[3:0] == `CPU_PC_REG) |
o_dbg_reg <= {{(32-AW){1'b0}},(i_dbg_reg[4])?upc:ipc}; |
else if (i_dbg_reg[3:0] == `CPU_CC_REG) |
begin |
o_dbg_reg[13:0] <= (i_dbg_reg[4])?w_uflags:w_iflags; |
o_dbg_reg[`CPU_GIE_BIT] <= gie; |
end |
end |
end else begin |
always @(posedge i_clk) |
begin |
o_dbg_reg <= regset[i_dbg_reg]; |
if (i_dbg_reg[3:0] == `CPU_PC_REG) |
o_dbg_reg <= (i_dbg_reg[4])?upc:ipc; |
else if (i_dbg_reg[3:0] == `CPU_CC_REG) |
begin |
o_dbg_reg[13:0] <= (i_dbg_reg[4])?w_uflags:w_iflags; |
o_dbg_reg[`CPU_GIE_BIT] <= gie; |
end |
end |
end endgenerate |
|
always @(posedge i_clk) |
o_dbg_cc <= { o_break, bus_err, gie, sleep }; |
|
always @(posedge i_clk) |
o_dbg_stall <= (i_halt)&&( |
(pf_cyc)||(mem_cyc_gbl)||(mem_cyc_lcl)||(mem_busy) |
||((~opvalid)&&(~i_rst)) |
||((~dcdvalid)&&(~i_rst))); |
|
// |
// |
// Produce accounting outputs: Account for any CPU stalls, so we can |
// later evaluate how well we are doing. |
// |
// |
assign o_op_stall = (master_ce)&&(op_stall); |
assign o_pf_stall = (master_ce)&&(~pf_valid); |
assign o_i_count = (alu_pc_valid)&&(~clear_pipeline); |
|
`ifdef DEBUG_SCOPE |
always @(posedge i_clk) |
o_debug <= { |
pf_pc[3:0], flags, |
pf_valid, dcdvalid, opvalid, alu_valid, mem_valid, |
op_ce, alu_ce, mem_ce, |
// |
master_ce, opvalid_alu, opvalid_mem, |
// |
alu_stall, mem_busy, op_pipe, mem_pipe_stalled, |
mem_we, |
// ((opvalid_alu)&&(alu_stall)) |
// ||((opvalid_mem)&&(~op_pipe)&&(mem_busy)) |
// ||((opvalid_mem)&&( op_pipe)&&(mem_pipe_stalled))); |
// opA[23:20], opA[3:0], |
gie, sleep, |
wr_reg_ce, wr_reg_vl[4:0] |
/* |
i_rst, master_ce, (new_pc), |
((dcd_early_branch)&&(dcdvalid)), |
pf_valid, pf_illegal, |
op_ce, dcd_ce, dcdvalid, dcd_stalled, |
pf_cyc, pf_stb, pf_we, pf_ack, pf_stall, pf_err, |
pf_pc[7:0], pf_addr[7:0] |
*/ |
}; |
`endif |
|
endmodule |
/trunk/rtl/cpu/wbdblpriarb.v
0,0 → 1,142
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: wbdblpriarb.v |
// |
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core |
// |
// Purpose: This should almost be identical to the priority arbiter, save |
// for a simple diffence: it allows the arbitration of two |
// separate wishbone buses. The purpose of this is to push the address |
// resolution back one cycle, so that by the first clock visible to this |
// core, it is known which of two parts of the bus the desired address |
// will be on, save that we still use the arbiter since the underlying |
// device doesn't know that there are two wishbone buses. |
// |
// So at this point we've deviated from the WB spec somewhat, by allowing |
// two CYC and two STB lines. Everything else is the same. This allows |
// (in this case the Zip CPU) to determine whether or not the access |
// will be to the local ZipSystem bus or the external WB bus on the clock |
// before the local bus access, otherwise peripherals were needing to do |
// multiple device selection comparisons/test within a clock: 1) is this |
// for the local or external bus, and 2) is this referencing me as a |
// peripheral. This then caused the ZipCPU to fail all timing specs. |
// By creating the two pairs of lines, CYC_A/STB_A and CYC_B/STB_B, the |
// determination of local vs external can be made one clock earlier |
// where there's still time for the logic, and the second comparison |
// now has time to complete. |
// |
// So let me try to explain this again. To use this arbiter, one of the |
// two masters sets CYC and STB before, only the master determines which |
// of two address spaces the CYC and STB apply to before the clock and |
// only sets the appropriate CYC and STB lines. Then, on the clock tick, |
// the arbiter determines who gets *both* busses, as they both share every |
// other WB line. Thus, only one of CYC_A and CYC_B going out will ever |
// be high at a given time. |
// |
// Hopefully this makes more sense than it sounds. If not, check out the |
// code below for a better explanation. |
// |
// 20150919 -- Added supported for the WB error signal. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
module wbdblpriarb(i_clk, i_rst, |
// Bus A |
i_a_cyc_a,i_a_cyc_b,i_a_stb_a,i_a_stb_b,i_a_we,i_a_adr, i_a_dat, o_a_ack, o_a_stall, o_a_err, |
// Bus B |
i_b_cyc_a,i_b_cyc_b,i_b_stb_a,i_b_stb_b,i_b_we,i_b_adr, i_b_dat, o_b_ack, o_b_stall, o_b_err, |
// Both buses |
o_cyc_a, o_cyc_b, o_stb_a, o_stb_b, o_we, o_adr, o_dat, |
i_ack, i_stall, i_err); |
parameter DW=32, AW=32; |
// Wishbone doesn't use an i_ce signal. While it could, they dislike |
// what it would (might) do to the synchronous reset signal, i_rst. |
input i_clk, i_rst; |
// Bus A |
input i_a_cyc_a, i_a_cyc_b, i_a_stb_a, i_a_stb_b, i_a_we; |
input [(AW-1):0] i_a_adr; |
input [(DW-1):0] i_a_dat; |
output wire o_a_ack, o_a_stall, o_a_err; |
// Bus B |
input i_b_cyc_a, i_b_cyc_b, i_b_stb_a, i_b_stb_b, i_b_we; |
input [(AW-1):0] i_b_adr; |
input [(DW-1):0] i_b_dat; |
output wire o_b_ack, o_b_stall, o_b_err; |
// |
output wire o_cyc_a,o_cyc_b, o_stb_a, o_stb_b, o_we; |
output wire [(AW-1):0] o_adr; |
output wire [(DW-1):0] o_dat; |
input i_ack, i_stall, i_err; |
|
// All of our logic is really captured in the 'r_a_owner' register. |
// This register determines who owns the bus. If no one is requesting |
// the bus, ownership goes to A on the next clock. Otherwise, if B is |
// requesting the bus and A is not, then ownership goes to not A on |
// the next clock. (Sounds simple ...) |
// |
// The CYC logic is here to make certain that, by the time we determine |
// who the bus owner is, we can do so based upon determined criteria. |
assign o_cyc_a = (~i_rst)&&((r_a_owner) ? i_a_cyc_a : i_b_cyc_a); |
assign o_cyc_b = (~i_rst)&&((r_a_owner) ? i_a_cyc_b : i_b_cyc_b); |
reg r_a_owner; |
initial r_a_owner = 1'b1; |
always @(posedge i_clk) |
if (i_rst) |
r_a_owner <= 1'b1; |
else if ((~o_cyc_a)&&(~o_cyc_b)) |
r_a_owner <= ((i_b_cyc_a)||(i_b_cyc_b))? 1'b0:1'b1; |
|
|
// Realistically, if neither master owns the bus, the output is a |
// don't care. Thus we trigger off whether or not 'A' owns the bus. |
// If 'B' owns it all we care is that 'A' does not. Likewise, if |
// neither owns the bus than the values on these various lines are |
// irrelevant. |
assign o_stb_a = (r_a_owner) ? i_a_stb_a : i_b_stb_a; |
assign o_stb_b = (r_a_owner) ? i_a_stb_b : i_b_stb_b; |
assign o_we = (r_a_owner) ? i_a_we : i_b_we; |
assign o_adr = (r_a_owner) ? i_a_adr : i_b_adr; |
assign o_dat = (r_a_owner) ? i_a_dat : i_b_dat; |
|
// We cannot allow the return acknowledgement to ever go high if |
// the master in question does not own the bus. Hence we force it |
// low if the particular master doesn't own the bus. |
assign o_a_ack = ( r_a_owner) ? i_ack : 1'b0; |
assign o_b_ack = (~r_a_owner) ? i_ack : 1'b0; |
|
// Stall must be asserted on the same cycle the input master asserts |
// the bus, if the bus isn't granted to him. |
assign o_a_stall = ( r_a_owner) ? i_stall : 1'b1; |
assign o_b_stall = (~r_a_owner) ? i_stall : 1'b1; |
|
// |
// These error lines will be implemented soon, as soon as the rest of |
// the Zip CPU is ready to support them. |
// |
assign o_a_err = ( r_a_owner) ? i_err : 1'b0; |
assign o_b_err = (~r_a_owner) ? i_err : 1'b0; |
|
endmodule |
|
/trunk/rtl/flashconfig.v
0,0 → 1,48
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: flashconfig.v |
// |
// Project: Wishbone Controlled Quad SPI Flash Controller |
// |
// Purpose: A configuration file, separated from the controller file, so |
// that multiple files can use the same wishbone Quad Spi Flash |
// controller, while each having a separate configuration. Currently, |
// the configuration only includes whether the flash is read only or not. |
// Other configuration options may be added later. |
// |
// |
// Creator: Dan Gisselquist |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
`ifndef FLASH_CONFIG_V |
`define FLASH_CONFIG_V |
// |
`define READ_ONLY |
// |
`endif |
// |
/trunk/rtl/wbqspiflash.v
0,0 → 1,1191
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: wbspiflash.v |
// |
// Project: Wishbone Controlled Quad SPI Flash Controller |
// |
// Purpose: Access a Quad SPI flash via a WISHBONE interface. This |
// includes both read and write (and erase) commands to the SPI |
// flash. All read/write commands are accomplished using the |
// high speed (4-bit) interface. Further, the device will be |
// left/kept in the 4-bit read interface mode between accesses, |
// for a minimum read latency. |
// |
// Wishbone Registers (See spec sheet for more detail): |
// 0: local config(r) / erase commands(w) / deep power down cmds / etc. |
// R: (Write in Progress), (dirty-block), (spi_port_busy), 1'b0, 9'h00, |
// { last_erased_sector, 14'h00 } if (WIP) |
// else { current_sector_being_erased, 14'h00 } |
// current if write in progress, last if written |
// W: (1'b1 to erase), (12'h ignored), next_erased_block, 14'h ignored) |
// 1: Configuration register |
// 2: Status register (R/w) |
// 3: Read ID (read only) |
// (19 bits): Data (R/w, but expect writes to take a while) |
// |
// |
// Creator: Dan Gisselquist |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
`include "flash_config.v" |
// |
`define WBQSPI_RESET 0 |
`define WBQSPI_RESET_QUADMODE 1 |
`define WBQSPI_IDLE 2 |
`define WBQSPI_RDIDLE 3 // Idle, but in fast read mode |
`define WBQSPI_WBDECODE 4 |
`define WBQSPI_RD_DUMMY 5 |
`define WBQSPI_QRD_ADDRESS 6 |
`define WBQSPI_QRD_DUMMY 7 |
`define WBQSPI_READ_CMD 8 |
`define WBQSPI_READ_DATA 9 |
`define WBQSPI_WAIT_TIL_RDIDLE 10 |
`define WBQSPI_READ_ID_CMD 11 |
`define WBQSPI_READ_ID 12 |
`define WBQSPI_READ_STATUS 13 |
`define WBQSPI_READ_CONFIG 14 |
`define WBQSPI_WAIT_TIL_IDLE 15 |
// |
// |
`ifndef READ_ONLY |
// |
`define WBQSPI_WAIT_WIP_CLEAR 16 |
`define WBQSPI_CHECK_WIP_CLEAR 17 |
`define WBQSPI_CHECK_WIP_DONE 18 |
`define WBQSPI_WEN 19 |
`define WBQSPI_PP 20 // Program page |
`define WBQSPI_QPP 21 // Program page, 4 bit mode |
`define WBQSPI_WR_DATA 22 |
`define WBQSPI_WR_BUS_CYCLE 23 |
`define WBQSPI_WRITE_STATUS 24 |
`define WBQSPI_WRITE_CONFIG 25 |
`define WBQSPI_ERASE_WEN 26 |
`define WBQSPI_ERASE_CMD 27 |
`define WBQSPI_ERASE_BLOCK 28 |
`define WBQSPI_CLEAR_STATUS 29 |
`define WBQSPI_IDLE_CHECK_WIP 30 |
// |
`endif |
|
module wbqspiflash(i_clk_100mhz, |
// Internal wishbone connections |
i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb, i_wb_we, |
i_wb_addr, i_wb_data, |
// Wishbone return values |
o_wb_ack, o_wb_stall, o_wb_data, |
// Quad Spi connections to the external device |
o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat, |
o_interrupt); |
parameter ADDRESS_WIDTH=22; |
input i_clk_100mhz; |
// Wishbone, inputs first |
input i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb, i_wb_we; |
input [(ADDRESS_WIDTH-3):0] i_wb_addr; |
input [31:0] i_wb_data; |
// then outputs |
output reg o_wb_ack; |
output reg o_wb_stall; |
output reg [31:0] o_wb_data; |
// Quad SPI control wires |
output wire o_qspi_sck, o_qspi_cs_n; |
output wire [1:0] o_qspi_mod; |
output wire [3:0] o_qspi_dat; |
input [3:0] i_qspi_dat; |
// Interrupt line |
output reg o_interrupt; |
// output wire [31:0] o_debug; |
|
reg spi_wr, spi_hold, spi_spd, spi_dir; |
reg [31:0] spi_in; |
reg [1:0] spi_len; |
wire [31:0] spi_out; |
wire spi_valid, spi_busy; |
wire w_qspi_sck, w_qspi_cs_n; |
wire [3:0] w_qspi_dat; |
wire [1:0] w_qspi_mod; |
// wire [22:0] spi_dbg; |
llqspi lldriver(i_clk_100mhz, |
spi_wr, spi_hold, spi_in, spi_len, spi_spd, spi_dir, |
spi_out, spi_valid, spi_busy, |
w_qspi_sck, w_qspi_cs_n, w_qspi_mod, w_qspi_dat, |
i_qspi_dat); |
|
// Erase status tracking |
reg write_in_progress, write_protect; |
reg [(ADDRESS_WIDTH-17):0] erased_sector; |
reg dirty_sector; |
initial begin |
write_in_progress = 1'b0; |
erased_sector = 0; |
dirty_sector = 1'b1; |
write_protect = 1'b1; |
end |
|
reg [7:0] last_status; |
reg quad_mode_enabled; |
reg spif_cmd, spif_override; |
reg [(ADDRESS_WIDTH-3):0] spif_addr; |
reg [31:0] spif_data; |
reg [5:0] state; |
reg spif_ctrl, spif_req; |
wire [(ADDRESS_WIDTH-17):0] spif_sector; |
assign spif_sector = spif_addr[(ADDRESS_WIDTH-3):14]; |
|
// assign o_debug = { spi_wr, spi_spd, spi_hold, state, spi_dbg }; |
|
initial state = `WBQSPI_RESET; |
initial o_wb_ack = 1'b0; |
initial o_wb_stall = 1'b1; |
initial spi_wr = 1'b0; |
initial spi_len = 2'b00; |
initial quad_mode_enabled = 1'b0; |
initial o_interrupt = 1'b0; |
always @(posedge i_clk_100mhz) |
begin |
spif_override <= 1'b0; |
if (state == `WBQSPI_RESET) |
begin |
// From a reset, we should |
// Enable the Quad I/O mode |
// Disable the Write protection bits in the status register |
// Chip should already be up and running, so we can start |
// immediately .... |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
spi_wr <= 1'b0; |
spi_hold <= 1'b0; |
spi_spd <= 1'b0; |
spi_dir <= 1'b0; |
last_status <= 8'h00; |
state <= `WBQSPI_RESET_QUADMODE; |
spif_req <= 1'b0; |
spif_override <= 1'b1; |
last_status <= 8'hfc; // |
// This guarantees that we aren't starting in quad |
// I/O mode, where the FPGA configuration scripts may |
// have left us. |
end else if (state == `WBQSPI_RESET_QUADMODE) |
begin |
// Okay, so here's the problem: we don't know whether or not |
// the Xilinx loader started us up in Quad Read I/O idle mode. |
// So, thus we need to |
// Not ready to handle the bus yet, so stall any requests |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
|
// Do something ... |
if (last_status == 8'h00) |
begin |
spif_override <= 1'b0; |
state <= `WBQSPI_IDLE; |
end else begin |
last_status <= last_status - 8'h1; |
spif_override <= 1'b1; |
spif_cmd <= last_status[3]; // Toggle CS_n |
spif_ctrl <= last_status[0]; // Toggle clock too |
end |
end else if (state == `WBQSPI_IDLE) |
begin |
o_interrupt <= 1'b0; |
o_wb_stall <= 1'b0; |
o_wb_ack <= 1'b0; |
spif_cmd <= i_wb_we; |
spif_addr <= i_wb_addr; |
spif_data <= i_wb_data; |
spif_ctrl <= (i_wb_ctrl_stb)&&(~i_wb_data_stb); |
spif_req <= (i_wb_ctrl_stb)||(i_wb_data_stb); |
spi_wr <= 1'b0; // Keep the port idle, unless told otherwise |
spi_hold <= 1'b0; |
spi_spd <= 1'b0; |
spi_dir <= 1'b0; // Write (for now, 'cause of cmd) |
// Data register access |
if ((i_wb_data_stb)&&(i_wb_cyc)) |
begin |
|
if (i_wb_we) // Request to write a page |
begin |
`ifdef READ_ONLY |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b0; |
end else |
`else |
if((~write_protect)&&(~write_in_progress)) |
begin // 00 |
spi_wr <= 1'b1; |
spi_len <= 2'b00; // 8 bits |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_WEN; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end else if (write_protect) |
begin // whether or not write-in_progress ... |
// Do nothing on a write protect |
// violation |
// |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b0; |
end else begin // write is in progress, wait |
// for it to complete |
state <= `WBQSPI_WAIT_WIP_CLEAR; |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
end else if (~write_in_progress) |
`endif |
begin // Read access, normal mode(s) |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
spi_wr <= 1'b1; // Write cmd to device |
if (quad_mode_enabled) |
begin |
spi_in <= { 8'heb, |
{(24-ADDRESS_WIDTH){1'b0}}, |
i_wb_addr[(ADDRESS_WIDTH-3):0], 2'b00 }; |
state <= `WBQSPI_QRD_ADDRESS; |
spi_len <= 2'b00; // single byte, cmd only |
end else begin |
spi_in <= { 8'h0b, |
{(24-ADDRESS_WIDTH){1'b0}}, |
i_wb_addr[(ADDRESS_WIDTH-3):0], 2'b00 }; |
state <= `WBQSPI_RD_DUMMY; |
spi_len <= 2'b11; // cmd+addr,32bits |
end |
`ifndef READ_ONLY |
end else begin |
// A write is in progress ... need to stall |
// the bus until the write is complete. |
state <= `WBQSPI_WAIT_WIP_CLEAR; |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
`endif |
end |
end else if ((i_wb_cyc)&&(i_wb_ctrl_stb)&&(i_wb_we)) |
begin |
`ifdef READ_ONLY |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b0; |
`else |
o_wb_stall <= 1'b1; |
case(i_wb_addr[1:0]) |
2'b00: begin // Erase command register |
write_protect <= ~i_wb_data[28]; |
o_wb_stall <= 1'b0; |
|
if((i_wb_data[31])&&(~write_in_progress)) |
begin |
// Command an erase--ack it immediately |
|
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b0; |
|
if ((i_wb_data[31])&&(~write_protect)) |
begin |
spi_wr <= 1'b1; |
spi_len <= 2'b00; |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_ERASE_CMD; |
o_wb_stall <= 1'b1; |
end |
end else if (i_wb_data[31]) |
begin |
state <= `WBQSPI_WAIT_WIP_CLEAR; |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b1; |
end else |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b0; |
end |
2'b01: begin |
// Write the configuration register |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b1; |
|
// Need to send a write enable command first |
spi_wr <= 1'b1; |
spi_len <= 2'b00; // 8 bits |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_WRITE_CONFIG; |
end |
2'b10: begin |
// Write the status register |
o_wb_ack <= 1'b1; // Ack immediately |
o_wb_stall <= 1'b1; // Stall other cmds |
// Need to send a write enable command first |
spi_wr <= 1'b1; |
spi_len <= 2'b00; // 8 bits |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_WRITE_STATUS; |
end |
2'b11: begin // Write the ID register??? makes no sense |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b0; |
end |
endcase |
`endif |
end else if ((i_wb_cyc)&&(i_wb_ctrl_stb)) // &&(~i_wb_we)) |
begin |
case(i_wb_addr[1:0]) |
2'b00: begin // Read local register |
if (write_in_progress) // Read status |
begin// register, is write still in progress? |
state <= `WBQSPI_READ_STATUS; |
spi_wr <= 1'b1; |
spi_len <= 2'b01;// 8 bits out, 8 bits in |
spi_in <= { 8'h05, 24'h00}; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end else begin // Return w/o talking to device |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b0; |
o_wb_data <= { write_in_progress, |
dirty_sector, spi_busy, |
~write_protect, |
quad_mode_enabled, |
{(29-ADDRESS_WIDTH){1'b0}}, |
erased_sector, 14'h000 }; |
end end |
2'b01: begin // Read configuration register |
state <= `WBQSPI_READ_CONFIG; |
spi_wr <= 1'b1; |
spi_len <= 2'b01; |
spi_in <= { 8'h35, 24'h00}; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
2'b10: begin // Read status register |
state <= `WBQSPI_READ_STATUS; |
spi_wr <= 1'b1; |
spi_len <= 2'b01; // 8 bits out, 8 bits in |
spi_in <= { 8'h05, 24'h00}; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
2'b11: begin // Read ID register |
state <= `WBQSPI_READ_ID_CMD; |
spi_wr <= 1'b1; |
spi_len <= 2'b00; |
spi_in <= { 8'h9f, 24'h00}; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
endcase |
`ifndef READ_ONLY |
end else if ((~i_wb_cyc)&&(write_in_progress)) |
begin |
state <= `WBQSPI_IDLE_CHECK_WIP; |
spi_wr <= 1'b1; |
spi_len <= 2'b01; // 8 bits out, 8 bits in |
spi_in <= { 8'h05, 24'h00}; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
`endif |
end |
end else if (state == `WBQSPI_RDIDLE) |
begin |
spi_wr <= 1'b0; |
o_wb_stall <= 1'b0; |
o_wb_ack <= 1'b0; |
spif_cmd <= i_wb_we; |
spif_addr <= i_wb_addr; |
spif_data <= i_wb_data; |
spif_ctrl <= (i_wb_ctrl_stb)&&(~i_wb_data_stb); |
spif_req <= (i_wb_ctrl_stb)||(i_wb_data_stb); |
spi_hold <= 1'b0; |
spi_spd<= 1'b1; |
spi_dir <= 1'b0; // Write (for now) |
if ((i_wb_cyc)&&(i_wb_data_stb)&&(~i_wb_we)) |
begin // Continue our read ... send the new address / mode |
o_wb_stall <= 1'b1; |
spi_wr <= 1'b1; |
spi_len <= 2'b10; // Write address, but not mode byte |
spi_in <= { {(24-ADDRESS_WIDTH){1'b0}}, |
i_wb_addr[(ADDRESS_WIDTH-3):0], 2'b00, 8'ha0 }; |
state <= `WBQSPI_QRD_DUMMY; |
end else if((i_wb_cyc)&&(i_wb_ctrl_stb)&&(~i_wb_we)&&(i_wb_addr[1:0] == 2'b00)) |
begin |
// A local read that doesn't touch the device, so leave |
// the device in its current state |
o_wb_stall <= 1'b0; |
o_wb_ack <= 1'b1; |
o_wb_data <= { write_in_progress, |
dirty_sector, spi_busy, |
~write_protect, |
quad_mode_enabled, |
{(29-ADDRESS_WIDTH){1'b0}}, |
erased_sector, 14'h000 }; |
end else if((i_wb_cyc)&&((i_wb_ctrl_stb)||(i_wb_data_stb))) |
begin // Need to release the device from quad mode for all else |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
spi_wr <= 1'b1; |
spi_len <= 2'b11; |
spi_in <= 32'h00; |
state <= `WBQSPI_WBDECODE; |
end |
end else if (state == `WBQSPI_WBDECODE) |
begin |
// We were in quad SPI read mode, and had to get out. |
// Now we've got a command (not data read) to read and |
// execute. Accomplish what we would've done while in the |
// IDLE state here, save only that we don't have to worry |
// about data reads, and we need to operate on a stored |
// version of the bus command |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
spi_wr <= 1'b0; // Keep the port idle, unless told otherwise |
spi_hold <= 1'b0; |
spi_spd <= 1'b0; |
spi_dir <= 1'b0; |
spif_req<= (spif_req) && (i_wb_cyc); |
if ((~spi_busy)&&(o_qspi_cs_n)&&(~spi_wr)) // only in full idle ... |
begin |
// Data register access |
if (~spif_ctrl) |
begin |
if (spif_cmd) // Request to write a page |
begin |
`ifdef READ_ONLY |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b0; |
state <= `WBQSPI_IDLE; |
`else |
if((~write_protect)&&(~write_in_progress)) |
begin // 00 |
spi_wr <= 1'b1; |
spi_len <= 2'b00; // 8 bits |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_WEN; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end else if (write_protect) |
begin // whether or not write-in_progress ... |
// Do nothing on a write protect |
// violation |
// |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b0; |
state <= `WBQSPI_IDLE; |
end else begin // write is in progress, wait |
// for it to complete |
state <= `WBQSPI_WAIT_WIP_CLEAR; |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
// end else if (~write_in_progress) // always true |
// but ... we wouldn't get here on a normal read access |
`endif |
end else begin |
// Something's wrong, we should never |
// get here |
// Attempt to go to idle to recover |
state <= `WBQSPI_IDLE; |
end |
end else if ((spif_ctrl)&&(spif_cmd)) |
begin |
`ifdef READ_ONLY |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b0; |
state <= `WBQSPI_IDLE; |
`else |
o_wb_stall <= 1'b1; |
case(spif_addr[1:0]) |
2'b00: begin // Erase command register |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b0; |
state <= `WBQSPI_IDLE; |
write_protect <= ~spif_data[28]; |
// Are we commanding an erase? |
// We're in read mode, writes cannot |
// be in progress, so ... |
if (spif_data[31]) // Command an erase |
begin |
// Since we're not going back |
// to IDLE, we must stall the |
// bus here |
o_wb_stall <= 1'b1; |
spi_wr <= 1'b1; |
spi_len <= 2'b00; |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_ERASE_CMD; |
end end |
2'b01: begin |
// Write the configuration register |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b1; |
|
// Need to send a write enable command first |
spi_wr <= 1'b1; |
spi_len <= 2'b00; // 8 bits |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_WRITE_CONFIG; |
end |
2'b10: begin |
// Write the status register |
o_wb_ack <= spif_req; // Ack immediately |
o_wb_stall <= 1'b1; // Stall other cmds |
// Need to send a write enable command first |
spi_wr <= 1'b1; |
spi_len <= 2'b00; // 8 bits |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_WRITE_STATUS; |
end |
2'b11: begin // Write the ID register??? makes no sense |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b0; |
state <= `WBQSPI_IDLE; |
end |
endcase |
`endif |
end else begin // on (~spif_we) |
case(spif_addr[1:0]) |
2'b00: begin // Read local register |
// Nonsense case--would've done this |
// already |
state <= `WBQSPI_IDLE; |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b0; |
end |
2'b01: begin // Read configuration register |
state <= `WBQSPI_READ_CONFIG; |
spi_wr <= 1'b1; |
spi_len <= 2'b01; |
spi_in <= { 8'h35, 24'h00}; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
2'b10: begin // Read status register |
state <= `WBQSPI_READ_STATUS; |
spi_wr <= 1'b1; |
spi_len <= 2'b01; // 8 bits out, 8 bits in |
spi_in <= { 8'h05, 24'h00}; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
2'b11: begin // Read ID register |
state <= `WBQSPI_READ_ID_CMD; |
spi_wr <= 1'b1; |
spi_len <= 2'b00; |
spi_in <= { 8'h9f, 24'h00}; |
|
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
endcase |
end |
end |
// |
// |
// READ DATA section: for both data and commands |
// |
end else if (state == `WBQSPI_RD_DUMMY) |
begin |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
|
spi_wr <= 1'b1; // Non-stop |
// Need to read one byte of dummy data, |
// just to consume 8 clocks |
spi_in <= { 8'h00, 24'h00 }; |
spi_len <= 2'b00; // Read 8 bits |
spi_spd <= 1'b0; |
spi_hold <= 1'b0; |
spif_req<= (spif_req) && (i_wb_cyc); |
|
if ((~spi_busy)&&(~o_qspi_cs_n)) |
// Our command was accepted |
state <= `WBQSPI_READ_CMD; |
end else if (state == `WBQSPI_QRD_ADDRESS) |
begin |
// We come in here immediately upon issuing a QRD read |
// command (8-bits), but we have to pause to give the |
// address (24-bits) and mode (8-bits) in quad speed. |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
|
spi_wr <= 1'b1; // Non-stop |
spi_in <= { {(24-ADDRESS_WIDTH){1'b0}}, |
spif_addr[(ADDRESS_WIDTH-3):0], 2'b00, 8'ha0 }; |
spi_len <= 2'b10; // Write address, not mode byte |
spi_spd <= 1'b1; |
spi_dir <= 1'b0; // Still writing |
spi_hold <= 1'b0; |
spif_req<= (spif_req) && (i_wb_cyc); |
|
if ((~spi_busy)&&(spi_spd)) |
// Our command was accepted |
state <= `WBQSPI_QRD_DUMMY; |
end else if (state == `WBQSPI_QRD_DUMMY) |
begin |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
|
spi_wr <= 1'b1; // Non-stop |
spi_in <= { 8'ha0, 24'h00 }; // Mode byte, then 2 bytes dummy |
spi_len <= 2'b10; // Write 8 bits |
spi_spd <= 1'b1; |
spi_dir <= 1'b0; // Still writing |
spi_hold <= 1'b0; |
spif_req<= (spif_req) && (i_wb_cyc); |
|
if ((~spi_busy)&&(spi_in[31:28] == 4'ha)) |
// Our command was accepted |
state <= `WBQSPI_READ_CMD; |
end else if (state == `WBQSPI_READ_CMD) |
begin // Issue our first command to read 32 bits. |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
|
spi_wr <= 1'b1; |
spi_in <= { 8'hff, 24'h00 }; // Empty |
spi_len <= 2'b11; // Read 32 bits |
spi_dir <= 1'b1; // Now reading |
spi_hold <= 1'b0; |
spif_req<= (spif_req) && (i_wb_cyc); |
if ((spi_valid)&&(spi_len == 2'b11)) |
state <= `WBQSPI_READ_DATA; |
end else if (state == `WBQSPI_READ_DATA) |
begin |
// Pipelined read support |
spi_wr <=((i_wb_cyc)&&(i_wb_data_stb)&&(~i_wb_we)&&(i_wb_addr== (spif_addr+1))); |
spi_in <= 32'h00; |
spi_len <= 2'b11; |
// Don't adjust the speed here, it was set in the setup |
spi_dir <= 1'b1; // Now we get to read |
// Don't let the device go to idle until the bus cycle ends. |
// This actually prevents a *really* nasty race condition, |
// where the strobe comes in after the lower level device |
// has decided to stop waiting. The write is then issued, |
// but no one is listening. By leaving the device open, |
// the device is kept in a state where a valid strobe |
// here will be useful. Of course, we don't accept |
// all commands, just reads. Further, the strobe needs |
// to be high for two clocks cycles without changing |
// anything on the bus--one for us to notice it and pull |
// our head out of the sand, and a second for whoever |
// owns the bus to realize their command went through. |
spi_hold <= 1'b1; |
spif_req<= (spif_req) && (i_wb_cyc); |
if ((spi_valid)&&(~spi_in[31])) |
begin // Single pulse acknowledge and write data out |
o_wb_ack <= spif_req; |
o_wb_stall <= (~spi_wr); |
// adjust endian-ness to match the PC |
o_wb_data <= { spi_out[7:0], spi_out[15:8], |
spi_out[23:16], spi_out[31:24] }; |
state <= (spi_wr)?`WBQSPI_READ_DATA |
: ((spi_spd) ? `WBQSPI_WAIT_TIL_RDIDLE : `WBQSPI_WAIT_TIL_IDLE); |
spif_req <= spi_wr; |
spi_hold <= (~spi_wr); |
if (spi_wr) |
spif_addr <= i_wb_addr; |
end else if (~i_wb_cyc) |
begin // FAIL SAFE: If the bus cycle ends, forget why we're |
// here, just go back to idle |
state <= ((spi_spd) ? `WBQSPI_WAIT_TIL_RDIDLE : `WBQSPI_WAIT_TIL_IDLE); |
spi_hold <= 1'b0; |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end else begin |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
end |
end else if (state == `WBQSPI_WAIT_TIL_RDIDLE) |
begin // Wait 'til idle, but then go to fast read idle instead of full |
spi_wr <= 1'b0; // idle |
spi_hold <= 1'b0; |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
spif_req <= 1'b0; |
if ((~spi_busy)&&(o_qspi_cs_n)&&(~spi_wr)) // Wait for a full |
begin // clearing of the SPI port before moving on |
state <= `WBQSPI_RDIDLE; |
o_wb_stall <= 1'b0; |
o_wb_ack <= 1'b0;// Shouldn't be acking anything here |
end |
end else if (state == `WBQSPI_READ_ID_CMD) |
begin // We came into here immediately after issuing a 0x9f command |
// Now we need to read 32 bits of data. Result should be |
// 0x0102154d (8'h manufacture ID, 16'h device ID, followed |
// by the number of extended bytes available 8'h4d). |
o_wb_ack <= 1'b0; |
o_wb_stall<= 1'b1; |
|
spi_wr <= 1'b1; // No data to send, but need four bytes, since |
spi_len <= 2'b11; // 32 bits of data are ... useful |
spi_in <= 32'h00; // Irrelevant |
spi_spd <= 1'b0; // Slow speed |
spi_dir <= 1'b1; // Reading |
spi_hold <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
if ((~spi_busy)&&(~o_qspi_cs_n)&&(spi_len == 2'b11)) |
// Our command was accepted, now go read the result |
state <= `WBQSPI_READ_ID; |
end else if (state == `WBQSPI_READ_ID) |
begin |
o_wb_ack <= 1'b0; // Assuming we're still waiting |
o_wb_stall <= 1'b1; |
|
spi_wr <= 1'b0; // No more writes, we've already written the cmd |
spi_hold <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
|
// Here, we just wait until the result comes back |
// The problem is, the result may be the previous result. |
// So we use spi_len as an indicator |
spi_len <= 2'b00; |
if((spi_valid)&&(spi_len==2'b00)) |
begin // Put the results out as soon as possible |
o_wb_data <= spi_out[31:0]; |
o_wb_ack <= spif_req; |
spif_req <= 1'b0; |
end else if ((~spi_busy)&&(o_qspi_cs_n)) |
begin |
state <= `WBQSPI_IDLE; |
o_wb_stall <= 1'b0; |
end |
end else if (state == `WBQSPI_READ_STATUS) |
begin // We enter after the command has been given, for now just |
// read and return |
spi_wr <= 1'b0; |
o_wb_ack <= 1'b0; |
spi_hold <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
if (spi_valid) |
begin |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b1; |
spif_req <= 1'b0; |
last_status <= spi_out[7:0]; |
write_in_progress <= spi_out[0]; |
if (spif_addr[1:0] == 2'b00) // Local read, checking |
begin // status, 'cause we're writing |
o_wb_data <= { spi_out[0], |
dirty_sector, spi_busy, |
~write_protect, |
quad_mode_enabled, |
{(29-ADDRESS_WIDTH){1'b0}}, |
erased_sector, 14'h000 }; |
end else begin |
o_wb_data <= { 24'h00, spi_out[7:0] }; |
end |
end |
|
if ((~spi_busy)&&(~spi_wr)) |
state <= `WBQSPI_IDLE; |
end else if (state == `WBQSPI_READ_CONFIG) |
begin // We enter after the command has been given, for now just |
// read and return |
spi_wr <= 1'b0; |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
spi_hold <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
|
if (spi_valid) |
begin |
o_wb_data <= { 24'h00, spi_out[7:0] }; |
quad_mode_enabled <= spi_out[1]; |
end |
|
if ((~spi_busy)&&(~spi_wr)) |
begin |
state <= `WBQSPI_IDLE; |
o_wb_ack <= spif_req; |
o_wb_stall <= 1'b0; |
spif_req <= 1'b0; |
end |
|
// |
// |
// Write/erase data section |
// |
`ifndef READ_ONLY |
end else if (state == `WBQSPI_WAIT_WIP_CLEAR) |
begin |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
spi_wr <= 1'b0; |
spif_req<= (spif_req) && (i_wb_cyc); |
if (~spi_busy) |
begin |
spi_wr <= 1'b1; |
spi_in <= { 8'h05, 24'h0000 }; |
spi_hold <= 1'b1; |
spi_len <= 2'b01; // 16 bits write, so we can read 8 |
state <= `WBQSPI_CHECK_WIP_CLEAR; |
spi_spd <= 1'b0; // Slow speed |
spi_dir <= 1'b0; |
end |
end else if (state == `WBQSPI_CHECK_WIP_CLEAR) |
begin |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
// Repeat as often as necessary until we are clear |
spi_wr <= 1'b1; |
spi_in <= 32'h0000; // Values here are actually irrelevant |
spi_hold <= 1'b1; |
spi_len <= 2'b00; // One byte at a time |
spi_spd <= 1'b0; // Slow speed |
spi_dir <= 1'b0; |
spif_req<= (spif_req) && (i_wb_cyc); |
if ((spi_valid)&&(~spi_out[0])) |
begin |
state <= `WBQSPI_CHECK_WIP_DONE; |
spi_wr <= 1'b0; |
spi_hold <= 1'b0; |
write_in_progress <= 1'b0; |
last_status <= spi_out[7:0]; |
end |
end else if (state == `WBQSPI_CHECK_WIP_DONE) |
begin |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
// Let's let the SPI port come back to a full idle, |
// and the chip select line go low before continuing |
spi_wr <= 1'b0; |
spi_len <= 2'b00; |
spi_hold <= 1'b0; |
spi_spd <= 1'b0; // Slow speed |
spi_dir <= 1'b0; |
spif_req<= (spif_req) && (i_wb_cyc); |
if ((o_qspi_cs_n)&&(~spi_busy)) // Chip select line is high, we can continue |
begin |
spi_wr <= 1'b0; |
spi_hold <= 1'b0; |
|
casez({ spif_cmd, spif_ctrl, spif_addr[1:0] }) |
4'b00??: begin // Read data from ... somewhere |
spi_wr <= 1'b1; // Write cmd to device |
if (quad_mode_enabled) |
begin |
spi_in <= { 8'heb, |
{(24-ADDRESS_WIDTH){1'b0}}, |
spif_addr[(ADDRESS_WIDTH-3):0], 2'b00 }; |
state <= `WBQSPI_QRD_ADDRESS; |
// spi_len <= 2'b00; // single byte, cmd only |
end else begin |
spi_in <= { 8'h0b, |
{(24-ADDRESS_WIDTH){1'b0}}, |
spif_addr[(ADDRESS_WIDTH-3):0], 2'b00 }; |
state <= `WBQSPI_RD_DUMMY; |
spi_len <= 2'b11; // Send cmd and addr |
end end |
4'b10??: begin // Write data to ... anywhere |
spi_wr <= 1'b1; |
spi_len <= 2'b00; // 8 bits |
// Send a write enable command |
spi_in <= { 8'h06, 24'h00 }; |
state <= `WBQSPI_WEN; |
end |
4'b0110: begin // Read status register |
state <= `WBQSPI_READ_STATUS; |
spi_wr <= 1'b1; |
spi_len <= 2'b01; // 8 bits out, 8 bits in |
spi_in <= { 8'h05, 24'h00}; |
end |
4'b0111: begin |
state <= `WBQSPI_READ_ID_CMD; |
spi_wr <= 1'b1; |
spi_len <= 2'b00; |
spi_in <= { 8'h9f, 24'h00}; |
end |
default: begin // |
o_wb_stall <= 1'b1; |
o_wb_ack <= spif_req; |
state <= `WBQSPI_WAIT_TIL_IDLE; |
end |
endcase |
// spif_cmd <= i_wb_we; |
// spif_addr <= i_wb_addr; |
// spif_data <= i_wb_data; |
// spif_ctrl <= (i_wb_ctrl_stb)&&(~i_wb_data_stb); |
// spi_wr <= 1'b0; // Keep the port idle, unless told otherwise |
end |
end else if (state == `WBQSPI_WEN) |
begin // We came here after issuing a write enable command |
spi_wr <= 1'b0; |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
spif_req<= (spif_req) && (i_wb_cyc); |
if ((~spi_busy)&&(o_qspi_cs_n)&&(~spi_wr)) // Let's come to a full stop |
state <= (quad_mode_enabled)?`WBQSPI_QPP:`WBQSPI_PP; |
// state <= `WBQSPI_PP; |
end else if (state == `WBQSPI_PP) |
begin // We come here under a full stop / full port idle mode |
// Issue our command immediately |
spi_wr <= 1'b1; |
spi_in <= { 8'h02, |
{(24-ADDRESS_WIDTH){1'b0}}, |
spif_addr[(ADDRESS_WIDTH-3):0], 2'b00 }; |
spi_len <= 2'b11; |
spi_hold <= 1'b1; |
spi_spd <= 1'b0; |
spi_dir <= 1'b0; // Writing |
spif_req<= (spif_req) && (i_wb_cyc); |
|
// Once we get busy, move on |
if (spi_busy) |
state <= `WBQSPI_WR_DATA; |
if (spif_sector == erased_sector) |
dirty_sector <= 1'b1; |
end else if (state == `WBQSPI_QPP) |
begin // We come here under a full stop / full port idle mode |
// Issue our command immediately |
spi_wr <= 1'b1; |
spi_in <= { 8'h32, |
{(24-ADDRESS_WIDTH){1'b0}}, |
spif_addr[(ADDRESS_WIDTH-3):0], 2'b00 }; |
spi_len <= 2'b11; |
spi_hold <= 1'b1; |
spi_spd <= 1'b0; |
spi_dir <= 1'b0; // Writing |
spif_req<= (spif_req) && (i_wb_cyc); |
|
// Once we get busy, move on |
if (spi_busy) |
begin |
// spi_wr is irrelevant here ... |
// Set the speed value once, but wait til we get busy |
// to do so. |
spi_spd <= 1'b1; |
state <= `WBQSPI_WR_DATA; |
end |
if (spif_sector == erased_sector) |
dirty_sector <= 1'b1; |
end else if (state == `WBQSPI_WR_DATA) |
begin |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
spi_wr <= 1'b1; // write without waiting |
spi_in <= { |
spif_data[ 7: 0], |
spif_data[15: 8], |
spif_data[23:16], |
spif_data[31:24] }; |
spi_len <= 2'b11; // Write 4 bytes |
spi_hold <= 1'b1; |
if (~spi_busy) |
begin |
o_wb_ack <= spif_req; // Ack when command given |
state <= `WBQSPI_WR_BUS_CYCLE; |
end |
spif_req<= (spif_req) && (i_wb_cyc); |
end else if (state == `WBQSPI_WR_BUS_CYCLE) |
begin |
o_wb_ack <= 1'b0; // Turn off our ack and stall flags |
o_wb_stall <= 1'b1; |
spi_wr <= 1'b0; |
spi_hold <= 1'b1; |
write_in_progress <= 1'b1; |
spif_req<= (spif_req) && (i_wb_cyc); |
if (~i_wb_cyc) |
begin |
state <= `WBQSPI_WAIT_TIL_IDLE; |
spi_hold <= 1'b0; |
end else if (spi_wr) |
begin // Give the SPI a chance to get busy on the last write |
// Do nothing here. |
end else if ((i_wb_data_stb)&&(i_wb_we) |
&&(i_wb_addr == (spif_addr+1)) |
&&(i_wb_addr[(ADDRESS_WIDTH-3):6]==spif_addr[(ADDRESS_WIDTH-3):6])) |
begin |
spif_cmd <= 1'b1; |
spif_data <= i_wb_data; |
spif_addr <= i_wb_addr; |
spif_ctrl <= 1'b0; |
spif_req<= 1'b1; |
// We'll keep the bus stalled on this request |
// for a while |
state <= `WBQSPI_WR_DATA; |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b0; |
end else if ((i_wb_data_stb|i_wb_ctrl_stb)&&(~o_wb_ack)) // Writing out of bounds |
begin |
spi_hold <= 1'b0; |
spi_wr <= 1'b0; |
state <= `WBQSPI_WAIT_TIL_IDLE; |
end // Otherwise we stay here |
end else if (state == `WBQSPI_WRITE_CONFIG) |
begin // We enter immediately after commanding a WEN |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
|
spi_len <= 2'b10; |
spi_in <= { 8'h01, last_status, spif_data[7:0], 8'h00 }; |
spi_wr <= 1'b0; |
spi_hold <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
if ((~spi_busy)&&(~spi_wr)) |
begin |
spi_wr <= 1'b1; |
state <= `WBQSPI_WAIT_TIL_IDLE; |
write_in_progress <= 1'b1; |
quad_mode_enabled <= spif_data[1]; |
end |
end else if (state == `WBQSPI_WRITE_STATUS) |
begin // We enter immediately after commanding a WEN |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
|
spi_len <= 2'b01; |
spi_in <= { 8'h01, spif_data[7:0], 16'h00 }; |
// last_status <= i_wb_data[7:0]; // We'll read this in a moment |
spi_wr <= 1'b0; |
spi_hold <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
if ((~spi_busy)&&(~spi_wr)) |
begin |
spi_wr <= 1'b1; |
last_status <= spif_data[7:0]; |
write_in_progress <= 1'b1; |
if(((last_status[6])||(last_status[5])) |
&&((~spif_data[6])&&(~spif_data[5]))) |
state <= `WBQSPI_CLEAR_STATUS; |
else |
state <= `WBQSPI_WAIT_TIL_IDLE; |
end |
end else if (state == `WBQSPI_ERASE_CMD) |
begin // Know that WIP is clear on entry, WEN has just been commanded |
spi_wr <= 1'b0; |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b1; |
spi_hold <= 1'b0; |
spi_spd <= 1'b0; |
spi_dir <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
|
// Here's the erase command |
spi_in <= { 8'hd8, 2'h0, spif_data[19:14], 14'h000, 2'b00 }; |
spi_len <= 2'b11; // 32 bit write |
// together with setting our copy of the WIP bit |
write_in_progress <= 1'b1; |
// keeping track of which sector we just erased |
erased_sector <= spif_data[(ADDRESS_WIDTH-3):14]; |
// and marking this erase sector as no longer dirty |
dirty_sector <= 1'b0; |
|
// Wait for a full stop before issuing this command |
if ((~spi_busy)&&(~spi_wr)&&(o_qspi_cs_n)) |
begin // When our command is accepted, move to the next state |
spi_wr <= 1'b1; |
state <= `WBQSPI_ERASE_BLOCK; |
end |
end else if (state == `WBQSPI_ERASE_BLOCK) |
begin |
spi_wr <= 1'b0; |
spi_hold <= 1'b0; |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
// When the port clears, we can head back to idle |
if ((~spi_busy)&&(~spi_wr)) |
begin |
o_wb_ack <= spif_req; |
state <= `WBQSPI_IDLE; |
end |
end else if (state == `WBQSPI_CLEAR_STATUS) |
begin // Issue a clear status command |
spi_wr <= 1'b1; |
spi_hold <= 1'b0; |
spi_len <= 2'b00; // 8 bit command |
spi_in <= { 8'h30, 24'h00 }; |
spi_spd <= 1'b0; |
spi_dir <= 1'b0; |
last_status[6:5] <= 2'b00; |
spif_req <= (spif_req) && (i_wb_cyc); |
if ((spi_wr)&&(~spi_busy)) |
state <= `WBQSPI_WAIT_TIL_IDLE; |
end else if (state == `WBQSPI_IDLE_CHECK_WIP) |
begin // We are now in read status register mode |
|
// No bus commands have (yet) been given |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
spif_req <= (spif_req) && (i_wb_cyc); |
|
// Stay in this mode unless/until we get a command, or |
// the write is over |
spi_wr <= (((~i_wb_cyc)||((~i_wb_data_stb)&&(~i_wb_ctrl_stb))) |
&&(write_in_progress)); |
spi_len <= 2'b00; // 8 bit reads |
spi_spd <= 1'b0; // SPI, not quad |
spi_dir <= 1'b1; // Read |
if (spi_valid) |
begin |
write_in_progress <= spi_out[0]; |
if ((~spi_out[0])&&(write_in_progress)) |
o_interrupt <= 1'b1; |
end else |
o_interrupt <= 1'b0; |
|
if ((~spi_wr)&&(~spi_busy)&&(o_qspi_cs_n)) |
begin // We can now go to idle and process a command |
o_wb_stall <= 1'b0; |
o_wb_ack <= 1'b0; |
state <= `WBQSPI_IDLE; |
end |
`endif // !READ_ONLY |
end else // if (state == `WBQSPI_WAIT_TIL_IDLE) or anything else |
begin |
spi_wr <= 1'b0; |
spi_hold <= 1'b0; |
o_wb_stall <= 1'b1; |
o_wb_ack <= 1'b0; |
spif_req <= 1'b0; |
if ((~spi_busy)&&(o_qspi_cs_n)&&(~spi_wr)) // Wait for a full |
begin // clearing of the SPI port before moving on |
state <= `WBQSPI_IDLE; |
o_wb_stall <= 1'b0; |
o_wb_ack <= 1'b0; // Shouldn't be acking anything here |
end |
end |
end |
|
// Command and control during the reset sequence |
assign o_qspi_cs_n = (spif_override)?spif_cmd :w_qspi_cs_n; |
assign o_qspi_sck = (spif_override)?spif_ctrl:w_qspi_sck; |
assign o_qspi_mod = (spif_override)? 2'b01 :w_qspi_mod; |
assign o_qspi_dat = (spif_override)? 4'b00 :w_qspi_dat; |
endmodule |
/trunk/rtl/wbicapesimple.v
0,0 → 1,170
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: wbicapesimple.v |
// |
// Project: Wishbone to ICAPE_SPARTAN6 interface conversion |
// |
// Purpose: This is a companion project to the ICAPE2 conversion, instead |
// involving a conversion from a 32-bit WISHBONE bus to read |
// and write the ICAPE_SPARTAN6 program. Since this is a simple interface |
// only, the smarts have been stripped out of it. Therefore, if you wish |
// to write to or read from particular registers, you will need to do all |
// the sequencing yourself. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
module wbicapesimple(i_clk, |
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, |
o_wb_ack, o_wb_stall, o_wb_data, icap_dbg); |
input i_clk; |
// Wishbone inputs |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input [15:0] i_wb_data; |
// Wishbone outputs |
output reg o_wb_ack; |
output wire o_wb_stall; |
output reg [15:0] o_wb_data; |
output wire [25:0] icap_dbg; |
|
|
// Divide our clock by 8. Thus, a 100 MHz clock with a 10 ns period |
// gets divided down to an 80 ns period, which is greater than the |
// minimum 50 ns as per the data sheet. |
reg [2:0] clk_divider; |
initial clk_divider=0; |
always @(posedge i_clk) |
clk_divider <= clk_divider + 3'h1; |
|
reg icap_posedge, icap_preedge; |
initial icap_posedge = 0; |
always @(posedge i_clk) |
icap_posedge <= (clk_divider == 3'b011); |
initial icap_preedge = 0; |
always @(posedge i_clk) |
icap_preedge <= (clk_divider == 3'b010); |
// icap_posedge <= (&clk_divider); |
|
wire icap_clk; |
assign icap_clk = clk_divider[2]; |
|
reg icap_ce_n, icap_we_n; |
reg [15:0] icap_data_i; |
|
|
wire icap_busy; |
wire [15:0] icap_data_o; |
ICAP_SPARTAN6 icap_inst( |
.BUSY(icap_busy), // Active high, low during all writes |
.O(icap_data_o), |
.CE(icap_ce_n), |
.CLK(icap_clk), |
.I(icap_data_i), |
.WRITE(icap_we_n)); |
|
// assign o_wb_stall = (i_wb_cyc)&&((~icap_posedge)||((~icap_ce_n)&&(icap_busy))); |
|
wire [15:0] brev_i_wb_data; |
assign brev_i_wb_data[ 0] = i_wb_data[ 7]; |
assign brev_i_wb_data[ 1] = i_wb_data[ 6]; |
assign brev_i_wb_data[ 2] = i_wb_data[ 5]; |
assign brev_i_wb_data[ 3] = i_wb_data[ 4]; |
assign brev_i_wb_data[ 4] = i_wb_data[ 3]; |
assign brev_i_wb_data[ 5] = i_wb_data[ 2]; |
assign brev_i_wb_data[ 6] = i_wb_data[ 1]; |
assign brev_i_wb_data[ 7] = i_wb_data[ 0]; |
// |
assign brev_i_wb_data[ 8] = i_wb_data[15]; |
assign brev_i_wb_data[ 9] = i_wb_data[14]; |
assign brev_i_wb_data[10] = i_wb_data[13]; |
assign brev_i_wb_data[11] = i_wb_data[12]; |
assign brev_i_wb_data[12] = i_wb_data[11]; |
assign brev_i_wb_data[13] = i_wb_data[10]; |
assign brev_i_wb_data[14] = i_wb_data[ 9]; |
assign brev_i_wb_data[15] = i_wb_data[ 8]; |
// |
|
initial icap_data_i = 0; |
always @(posedge i_clk) |
if (icap_preedge) |
begin |
if (~i_wb_cyc) |
icap_data_i <= 16'hffff; |
else if ((icap_ce_n)||(~icap_busy)) |
icap_data_i <= brev_i_wb_data[15:0]; |
end |
|
initial icap_we_n = 1'b1; |
always @(posedge i_clk) |
if ((icap_preedge)&&((icap_ce_n)||(~icap_busy))) |
icap_we_n <= ~i_wb_we; |
initial icap_ce_n = 1'b1; |
always @(posedge i_clk) |
if ((icap_preedge)&&((icap_ce_n)||(~icap_busy))) |
icap_ce_n <= ~((i_wb_cyc)&&(i_wb_stb)); |
else if ((icap_preedge)&&(~i_wb_cyc)) |
icap_ce_n <= 1'b1; |
//initial r_wb_stall = 1'b0; |
//always @(posedge i_clk) |
//r_wb_stall <= (~icap_posedge)&&( |
//&&(icap_preedge)&&(~icap_busy)); |
// assign o_wb_stall = (i_wb_cyc)&&((~icap_posedge)||((~icap_ce_n)&&(icap_busy))); |
assign o_wb_stall = (~icap_posedge) |
||((i_wb_cyc)&&(~icap_ce_n)&&(icap_busy)); |
initial o_wb_ack = 1'b0; |
always @(posedge i_clk) |
o_wb_ack <= ((~icap_ce_n)&&(i_wb_cyc) |
&&(icap_preedge)&&(~icap_busy)); |
|
wire [15:0] brev_icap_data; |
assign brev_icap_data[ 0] = icap_data_o[ 7]; |
assign brev_icap_data[ 1] = icap_data_o[ 6]; |
assign brev_icap_data[ 2] = icap_data_o[ 5]; |
assign brev_icap_data[ 3] = icap_data_o[ 4]; |
assign brev_icap_data[ 4] = icap_data_o[ 3]; |
assign brev_icap_data[ 5] = icap_data_o[ 2]; |
assign brev_icap_data[ 6] = icap_data_o[ 1]; |
assign brev_icap_data[ 7] = icap_data_o[ 0]; |
// |
assign brev_icap_data[ 8] = icap_data_o[15]; |
assign brev_icap_data[ 9] = icap_data_o[14]; |
assign brev_icap_data[10] = icap_data_o[13]; |
assign brev_icap_data[11] = icap_data_o[12]; |
assign brev_icap_data[12] = icap_data_o[11]; |
assign brev_icap_data[13] = icap_data_o[10]; |
assign brev_icap_data[14] = icap_data_o[ 9]; |
assign brev_icap_data[15] = icap_data_o[ 8]; |
// |
initial o_wb_data = 16'h000; |
always @(posedge i_clk) |
if ((icap_posedge)&&((icap_ce_n)||(~icap_busy))) |
o_wb_data <= brev_icap_data; |
|
assign icap_dbg[25:0] = { |
i_wb_cyc,i_wb_stb, i_wb_we, o_wb_ack, o_wb_stall, |
icap_posedge, icap_clk, icap_ce_n, icap_busy, icap_we_n, |
(icap_we_n)?icap_data_o : icap_data_i }; |
|
endmodule |
|
/trunk/rtl/rxuart.v
0,0 → 1,304
///////////////////////////////////////////////////////////////////////// |
// |
// |
// Filename: rxuart.v |
// |
// Project: FPGA library development (Spartan 3E development board) |
// |
// Purpose: Receive and decode inputs from a single UART line. |
// |
// |
// To interface with this module, connect it to your system clock, |
// pass it the 32 bit setup register (defined below) and the UART |
// input. When data becomes available, the o_wr line will be asserted |
// for one clock cycle. On parity or frame errors, the o_parity_err |
// or o_frame_err lines will be asserted. Likewise, on a break |
// condition, o_break will be asserted. These lines are self clearing. |
// |
// There is a synchronous reset line, logic high. |
// |
// Now for the setup register. The register is 32 bits, so that this |
// UART may be set up over a 32-bit bus. |
// |
// i_setup[29:28] Indicates the number of data bits per word. This will |
// either be 2'b00 for an 8-bit word, 2'b01 for a 7-bit word, 2'b10 |
// for a six bit word, or 2'b11 for a five bit word. |
// |
// i_setup[27] Indicates whether or not to use one or two stop bits. |
// Set this to one to expect two stop bits, zero for one. |
// |
// i_setup[26] Indicates whether or not a parity bit exists. Set this |
// to 1'b1 to include parity. |
// |
// i_setup[25] Indicates whether or not the parity bit is fixed. Set |
// to 1'b1 to include a fixed bit of parity, 1'b0 to allow the |
// parity to be set based upon data. (Both assume the parity |
// enable value is set.) |
// |
// i_setup[24] This bit is ignored if parity is not used. Otherwise, |
// in the case of a fixed parity bit, this bit indicates whether |
// mark (1'b1) or space (1'b0) parity is used. Likewise if the |
// parity is not fixed, a 1'b1 selects even parity, and 1'b0 |
// selects odd. |
// |
// i_setup[23:0] Indicates the speed of the UART in terms of clocks. |
// So, for example, if you have a 200 MHz clock and wish to |
// run your UART at 9600 baud, you would take 200 MHz and divide |
// by 9600 to set this value to 24'd20834. Likewise if you wished |
// to run this serial port at 115200 baud from a 200 MHz clock, |
// you would set the value to 24'd1736 |
// |
// Thus, to set the UART for the common setting of an 8-bit word, |
// one stop bit, no parity, and 115200 baud over a 200 MHz clock, you |
// would want to set the setup value to: |
// |
// 32'h0006c8 // For 115,200 baud, 8 bit, no parity |
// 32'h005161 // For 9600 baud, 8 bit, no parity |
// |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
// Copyright: 2015 |
// |
// |
///////////////////////////////////////////////////////////////////////// |
// |
// This software is the ownership of Gisselquist Technology, LLC, and as |
// such it is proprietary. It is provided without any warrantees, either |
// express or implied, so that it may be tested. Upon completion, I ask |
// that working code be returned and not further distributed beyond those |
// that it is originally offered to. |
// |
// Thank you. |
// |
|
// States: (@ baud counter == 0) |
// 0 First bit arrives |
// ..7 Bits arrive |
// 8 Stop bit (x1) |
// 9 Stop bit (x2) |
/// c break condition |
// d Waiting for the channel to go high |
// e Waiting for the reset to complete |
// f Idle state |
`define RXU_BIT_ZERO 4'h0 |
`define RXU_BIT_ONE 4'h1 |
`define RXU_BIT_TWO 4'h2 |
`define RXU_BIT_THREE 4'h3 |
`define RXU_BIT_FOUR 4'h4 |
`define RXU_BIT_FIVE 4'h5 |
`define RXU_BIT_SIX 4'h6 |
`define RXU_BIT_SEVEN 4'h7 |
`define RXU_PARITY 4'h8 |
`define RXU_STOP 4'h9 |
`define RXU_SECOND_STOP 4'ha |
// Unused 4'hb |
// Unused 4'hc |
`define RXU_BREAK 4'hd |
`define RXU_RESET_IDLE 4'he |
`define RXU_IDLE 4'hf |
|
module rxuart(i_clk, i_reset, i_setup, i_uart, o_wr, o_data, o_break, |
o_parity_err, o_frame_err, o_ck_uart); |
// parameter // CLOCKS_PER_BAUD = 25'd004340, |
// BREAK_CONDITION = CLOCKS_PER_BAUD * 12, |
// CLOCKS_PER_HALF_BAUD = CLOCKS_PER_BAUD/2; |
// 8 data bits, no parity, (at least 1) stop bit |
input i_clk, i_reset; |
input [29:0] i_setup; |
input i_uart; |
output reg o_wr; |
output reg [7:0] o_data; |
output reg o_break; |
output reg o_parity_err, o_frame_err; |
output wire o_ck_uart; |
|
|
wire [27:0] clocks_per_baud, break_condition, half_baud; |
wire [1:0] data_bits; |
wire use_parity, parity_even, dblstop, fixd_parity; |
reg [29:0] r_setup; |
assign clocks_per_baud = { 4'h0, r_setup[23:0] }; |
assign data_bits = r_setup[29:28]; |
assign dblstop = r_setup[27]; |
assign use_parity = r_setup[26]; |
assign fixd_parity = r_setup[25]; |
assign parity_even = r_setup[24]; |
assign break_condition = { r_setup[23:0], 4'h0 }; |
assign half_baud = { 5'h00, r_setup[23:1] }; |
|
reg q_uart, qq_uart, ck_uart; |
initial q_uart = 1'b0; |
initial qq_uart = 1'b0; |
initial ck_uart = 1'b0; |
always @(posedge i_clk) |
begin |
q_uart <= i_uart; |
qq_uart <= q_uart; |
ck_uart <= qq_uart; |
end |
assign o_ck_uart = ck_uart; |
|
reg [27:0] chg_counter; |
initial chg_counter = 28'h00; |
always @(posedge i_clk) |
if (i_reset) |
chg_counter <= 28'h00; |
else if (qq_uart != ck_uart) |
chg_counter <= 28'h00; |
else if (chg_counter < break_condition) |
chg_counter <= chg_counter + 1; |
|
always @(posedge i_clk) |
o_break <=((chg_counter >= break_condition)&&(~ck_uart))? 1'b1:1'b0; |
|
reg [3:0] state; |
reg [27:0] baud_counter; |
reg [7:0] data_reg; |
reg calc_parity; |
initial o_wr = 1'b0; |
initial state = `RXU_RESET_IDLE; |
initial o_parity_err = 1'b0; |
initial o_frame_err = 1'b0; |
// initial baud_counter = clocks_per_baud; |
always @(posedge i_clk) |
begin |
if (i_reset) |
begin |
o_wr <= 1'b0; |
o_data <= 8'h00; |
state <= `RXU_RESET_IDLE; |
baud_counter <= clocks_per_baud; // Set, not reset |
data_reg <= 8'h00; |
calc_parity <= 1'b0; |
o_parity_err <= 1'b0; |
o_frame_err <= 1'b0; |
end else if (state == `RXU_RESET_IDLE) |
begin |
r_setup <= i_setup; |
data_reg <= 8'h00; o_data <= 8'h00; o_wr <= 1'b0; |
baud_counter <= clocks_per_baud-28'h01;// Set, not reset |
if ((ck_uart)&&(chg_counter >= break_condition)) |
// Goto idle state from a reset |
state <= `RXU_IDLE; |
else // Otherwise, stay in this condition 'til reset |
state <= `RXU_RESET_IDLE; |
calc_parity <= 1'b0; |
o_parity_err <= 1'b0; |
o_frame_err <= 1'b0; |
end else if ((~ck_uart)&&(chg_counter >= break_condition)) |
begin // We are in a break condition |
state <= `RXU_BREAK; |
o_wr <= 1'b0; |
o_data <= 8'h00; |
baud_counter <= clocks_per_baud-28'h01;// Set, not reset |
data_reg <= 8'h00; |
calc_parity <= 1'b0; |
o_parity_err <= 1'b0; |
o_frame_err <= 1'b0; |
r_setup <= i_setup; |
end else if (state == `RXU_BREAK) |
begin // Goto idle state following return ck_uart going high |
data_reg <= 8'h00; o_data <= 8'h00; o_wr <= 1'b0; |
baud_counter <= clocks_per_baud - 28'h01; |
if (ck_uart) |
state <= `RXU_IDLE; |
else |
state <= `RXU_BREAK; |
calc_parity <= 1'b0; |
o_parity_err <= 1'b0; |
o_frame_err <= 1'b0; |
r_setup <= i_setup; |
end else if (state == `RXU_IDLE) |
begin // Idle state, independent of baud counter |
data_reg <= 8'h00; o_data <= 8'h00; o_wr <= 1'b0; |
baud_counter <= clocks_per_baud - 28'h01; |
if ((ck_uart == 1'b0)&&(chg_counter > half_baud)) |
begin |
// We are in the center of a valid start bit |
case (data_bits) |
2'b00: state <= `RXU_BIT_ZERO; |
2'b01: state <= `RXU_BIT_ONE; |
2'b10: state <= `RXU_BIT_TWO; |
2'b11: state <= `RXU_BIT_THREE; |
endcase |
end else // Otherwise, just stay here in idle |
state <= `RXU_IDLE; |
calc_parity <= 1'b0; |
o_parity_err <= 1'b0; |
o_frame_err <= 1'b0; |
end else if (baud_counter == 0) |
begin |
baud_counter <= clocks_per_baud-28'h1; |
if (state < `RXU_BIT_SEVEN) |
begin |
// Data arrives least significant bit first. |
// By the time this is clocked in, it's what |
// you'll have. |
data_reg <= { ck_uart, data_reg[7:1] }; |
calc_parity <= calc_parity ^ ck_uart; |
o_data <= 8'h00; |
o_wr <= 1'b0; |
state <= state + 1; |
o_parity_err <= 1'b0; |
o_frame_err <= 1'b0; |
end else if (state == `RXU_BIT_SEVEN) |
begin |
data_reg <= { ck_uart, data_reg[7:1] }; |
calc_parity <= calc_parity ^ ck_uart; |
o_data <= 8'h00; |
o_wr <= 1'b0; |
state <= (use_parity) ? `RXU_PARITY:`RXU_STOP; |
o_parity_err <= 1'b0; |
o_frame_err <= 1'b0; |
end else if (state == `RXU_PARITY) |
begin |
if (fixd_parity) |
o_parity_err <= (ck_uart ^ parity_even); |
else |
o_parity_err <= ((parity_even && (calc_parity != ck_uart)) |
||((~parity_even)&&(calc_parity==ck_uart))); |
state <= `RXU_STOP; |
o_frame_err <= 1'b0; |
end else if (state == `RXU_STOP) |
begin // Stop (or parity) bit(s) |
case (data_bits) |
2'b00: o_data <= data_reg; |
2'b01: o_data <= { 1'b0, data_reg[7:1] }; |
2'b10: o_data <= { 2'b0, data_reg[7:2] }; |
2'b11: o_data <= { 3'b0, data_reg[7:3] }; |
endcase |
o_wr <= 1'b1; // Pulse the write |
o_frame_err <= (~ck_uart); |
if (~ck_uart) |
state <= `RXU_RESET_IDLE; |
else if (dblstop) |
state <= `RXU_SECOND_STOP; |
else |
state <= `RXU_IDLE; |
// o_parity_err <= 1'b0; |
end else // state must equal RX_SECOND_STOP |
begin |
if (~ck_uart) |
begin |
o_frame_err <= 1'b1; |
state <= `RXU_RESET_IDLE; |
end else begin |
state <= `RXU_IDLE; |
o_frame_err <= 1'b0; |
end |
o_parity_err <= 1'b0; |
end |
end else begin |
o_wr <= 1'b0; // data_reg = data_reg |
baud_counter <= baud_counter - 28'd1; |
o_parity_err <= 1'b0; |
o_frame_err <= 1'b0; |
end |
end |
|
endmodule |
|
|
/trunk/rtl/rtcdate.v
0,0 → 1,188
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: rtcdate.v |
// |
// Project: A Wishbone Controlled Real--time Clock Core |
// |
// Purpose: |
// This core provides a real-time date function that can be coupled with |
// a real-time clock. The date provided is in Binary Coded Decimal (bcd) |
// form, and available for reading and writing over the Wishbone Bus. |
// |
// WARNING: Race conditions exist when updating the date across the Wishbone |
// bus at or near midnight. (This should be obvious, but it bears |
// stating.) Specifically, if the update command shows up at the same |
// clock as the ppd clock, then the ppd clock will be ignored and the |
// new date will be the date of the day following midnight. However, |
// if the update command shows up one clock before the ppd, then the date |
// may be updated, but may have problems dealing with the last day of the |
// month or year. To avoid race conditions, update the date sometime |
// after the stroke of midnight and before 5 clocks before the next |
// midnight. If you are concerned that you might hit a race condition, |
// just read the clock again (5+ clocks later) to make certain you set |
// it correctly. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory. Run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
module rtcdate(i_clk, i_ppd, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, |
o_wb_ack, o_wb_stall, o_wb_data); |
input i_clk; |
// A one part per day signal, i.e. basically a clock enable line that |
// controls when the beginning of the day happens. This line should |
// be high on the very last second of any day in order for the rtcdate |
// module to always have the right date. |
input i_ppd; |
// Wishbone inputs |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input [31:0] i_wb_data; |
// Wishbone outputs |
output reg o_wb_ack; |
output wire o_wb_stall; |
output wire [31:0] o_wb_data; |
|
|
reg [5:0] r_day; |
reg [4:0] r_mon; |
reg [13:0] r_year; |
|
reg last_day_of_month, last_day_of_year, is_leap_year; |
always @(posedge i_clk) |
last_day_of_year <= (last_day_of_month) && (r_mon == 5'h12); |
always @(posedge i_clk) |
begin |
case(r_mon) |
5'h01: last_day_of_month <= (r_day >= 6'h31); // Jan |
5'h02: last_day_of_month <= (r_day >= 6'h29) |
||((~is_leap_year)&&(r_day == 6'h28)); |
5'h03: last_day_of_month <= (r_day >= 6'h31); // March |
5'h04: last_day_of_month <= (r_day >= 6'h30); // April |
5'h05: last_day_of_month <= (r_day >= 6'h31); // May |
5'h06: last_day_of_month <= (r_day >= 6'h30); // June |
5'h07: last_day_of_month <= (r_day >= 6'h31); // July |
5'h08: last_day_of_month <= (r_day >= 6'h31); // August |
5'h09: last_day_of_month <= (r_day >= 6'h30); // Sept |
5'h10: last_day_of_month <= (r_day >= 6'h31); // October |
5'h11: last_day_of_month <= (r_day >= 6'h30); // November |
5'h12: last_day_of_month <= (r_day >= 6'h31); // December |
default: last_day_of_month <= 1'b0; |
endcase |
end |
|
reg year_divisible_by_four, century_year, four_century_year; |
always @(posedge i_clk) |
year_divisible_by_four<= ((~r_year[0])&&(r_year[4]==r_year[1])); |
always @(posedge i_clk) |
century_year <= (r_year[7:0] == 8'h00); |
always @(posedge i_clk) |
four_century_year <= ((~r_year[8])&&((r_year[12]==r_year[9]))); |
always @(posedge i_clk) |
is_leap_year <= (year_divisible_by_four)&&((~century_year) |
||((century_year)&&(four_century_year))); |
|
|
// Adjust the day of month |
initial r_day = 6'h01; |
always @(posedge i_clk) |
begin |
if ((r_day == 0)||(r_day > 6'h31)||(r_day[3:0] > 4'h9)) |
r_day <= 6'h01; |
else if ((i_ppd)&&(last_day_of_month)) |
r_day <= 6'h01; |
else if ((i_ppd)&&(r_day[3:0] != 4'h9)) |
r_day[3:0] <= r_day[3:0] + 4'h1; |
else if (i_ppd) |
begin |
r_day[3:0] <= 4'h0; |
r_day[5:4] <= r_day[5:4] + 2'h1; |
end |
|
if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)&&(i_wb_data[7:0]!=8'hff)) |
r_day <= i_wb_data[5:0]; |
end |
|
// Adjust the month of the year |
initial r_mon = 5'h01; |
always @(posedge i_clk) |
begin |
if ((r_mon == 0)||(r_mon > 5'h12)||(r_mon[3:0] > 4'h9)) |
r_mon <= 5'h01; |
else if ((i_ppd)&&(last_day_of_year)) |
r_mon <= 5'h01; |
else if ((i_ppd)&&(last_day_of_month)&&(r_mon[3:0] != 4'h9)) |
r_mon[3:0] <= r_mon[3:0] + 4'h1; |
else if ((i_ppd)&&(last_day_of_month)) |
begin |
r_mon[3:0] <= 4'h0; |
r_mon[4] <= 1; |
end |
|
if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)&&(i_wb_data[15:8]!=8'hff)) |
r_mon <= i_wb_data[12:8]; |
end |
|
// Adjust the year |
initial r_year = 14'h2000; |
always @(posedge i_clk) |
begin |
// Deal with any out of bounds conditions |
if (r_year[3:0] > 4'h9) |
r_year[3:0] <= 4'h0; |
if (r_year[7:4] > 4'h9) |
r_year[7:4] <= 4'h0; |
if (r_year[11:8] > 4'h9) |
r_year[11:8] <= 4'h0; |
if ((i_ppd)&&(last_day_of_year)) |
begin |
if (r_year[3:0] != 4'h9) |
r_year[3:0] <= r_year[3:0] + 4'h1; |
else begin |
r_year[3:0] <= 4'h0; |
if (r_year[7:4] != 4'h9) |
r_year[7:4] <= r_year[7:4] + 4'h1; |
else begin |
r_year[7:4] <= 4'h0; |
if (r_year[11:8] != 4'h9) |
r_year[11:8] <= r_year[11:8]+4'h1; |
else begin |
r_year[11:8] <= 4'h0; |
r_year[13:12] <= r_year[13:12]+2'h1; |
end |
end |
end |
end |
|
if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)&&(i_wb_data[31:16]!=16'hffff)) |
r_year <= i_wb_data[29:16]; |
end |
|
always @(posedge i_clk) |
o_wb_ack <= ((i_wb_cyc)&&(i_wb_stb)); |
assign o_wb_stall = 1'b0; |
assign o_wb_data = { 2'h0, r_year, 3'h0, r_mon, 2'h0, r_day }; |
endmodule |
/trunk/rtl/txuart.v
0,0 → 1,217
///////////////////////////////////////////////////////////////////////// |
// |
// |
// Filename: txuart.v |
// |
// Project: FPGA library development (Spartan 3E development board) |
// |
// Purpose: Transmit outputs over a single UART line. |
// |
// To interface with this module, connect it to your system clock, |
// pass it the 32 bit setup register (defined below) and the byte |
// of data you wish to transmit. Strobe the i_wr line high for one |
// clock cycle, and your data will be off. Wait until the 'o_busy' |
// line is low before strobing the i_wr line again--this implementation |
// has NO BUFFER, so strobing i_wr while the core is busy will just |
// cause your data to be lost. The output will be placed on the o_txuart |
// output line. If you wish to set/send a break condition, assert the |
// i_break line otherwise leave it low. |
// |
// There is a synchronous reset line, logic high. |
// |
// Now for the setup register. The register is 32 bits, so that this |
// UART may be set up over a 32-bit bus. |
// |
// i_setup[29:28] Indicates the number of data bits per word. This will |
// either be 2'b00 for an 8-bit word, 2'b01 for a 7-bit word, 2'b10 |
// for a six bit word, or 2'b11 for a five bit word. |
// |
// i_setup[27] Indicates whether or not to use one or two stop bits. |
// Set this to one to expect two stop bits, zero for one. |
// |
// i_setup[26] Indicates whether or not a parity bit exists. Set this |
// to 1'b1 to include parity. |
// |
// i_setup[25] Indicates whether or not the parity bit is fixed. Set |
// to 1'b1 to include a fixed bit of parity, 1'b0 to allow the |
// parity to be set based upon data. (Both assume the parity |
// enable value is set.) |
// |
// i_setup[24] This bit is ignored if parity is not used. Otherwise, |
// in the case of a fixed parity bit, this bit indicates whether |
// mark (1'b1) or space (1'b0) parity is used. Likewise if the |
// parity is not fixed, a 1'b1 selects even parity, and 1'b0 |
// selects odd. |
// |
// i_setup[23:0] Indicates the speed of the UART in terms of clocks. |
// So, for example, if you have a 200 MHz clock and wish to |
// run your UART at 9600 baud, you would take 200 MHz and divide |
// by 9600 to set this value to 24'd20834. Likewise if you wished |
// to run this serial port at 115200 baud from a 200 MHz clock, |
// you would set the value to 24'd1736 |
// |
// Thus, to set the UART for the common setting of an 8-bit word, |
// one stop bit, no parity, and 115200 baud over a 200 MHz clock, you |
// would want to set the setup value to: |
// |
// 32'h0006c8 // For 115,200 baud, 8 bit, no parity |
// 32'h005161 // For 9600 baud, 8 bit, no parity |
// |
// Creator: Dan Gisselquist |
// Gisselquist Technology, LLC |
// |
// Copyright: 2015 |
// |
// |
///////////////////////////////////////////////////////////////////////// |
// |
// This software is the ownership of Gisselquist Technology, LLC, and as |
// such it is proprietary. It is provided without any warrantees, either |
// express or implied, so that it may be tested. Upon completion, I ask |
// that working code be returned and not further distributed beyond those |
// that it is originally offered to. |
// |
// Thank you. |
// |
`define TXU_BIT_ZERO 4'h0 |
`define TXU_BIT_ONE 4'h1 |
`define TXU_BIT_TWO 4'h2 |
`define TXU_BIT_THREE 4'h3 |
`define TXU_BIT_FOUR 4'h4 |
`define TXU_BIT_FIVE 4'h5 |
`define TXU_BIT_SIX 4'h6 |
`define TXU_BIT_SEVEN 4'h7 |
`define TXU_PARITY 4'h8 // Constant 1 |
`define TXU_STOP 4'h9 // Constant 1 |
`define TXU_SECOND_STOP 4'ha |
// 4'hb // Unused |
// 4'hc // Unused |
// `define TXU_START 4'hd // An unused state |
`define TXU_BREAK 4'he |
`define TXU_IDLE 4'hf |
|
module txuart(i_clk, i_reset, i_setup, i_break, i_wr, i_data, o_uart, o_busy); |
input i_clk, i_reset; |
input [29:0] i_setup; |
input i_break; |
input i_wr; |
input [7:0] i_data; |
output reg o_uart, o_busy; |
|
wire [27:0] clocks_per_baud, break_condition; |
wire [1:0] data_bits; |
wire use_parity, parity_even, dblstop, fixd_parity; |
reg [29:0] r_setup; |
assign clocks_per_baud = { 4'h0, r_setup[23:0] }; |
assign break_condition = { r_setup[23:0], 4'h0 }; |
assign data_bits = r_setup[29:28]; |
assign dblstop = r_setup[27]; |
assign use_parity = r_setup[26]; |
assign fixd_parity = r_setup[25]; |
assign parity_even = r_setup[24]; |
|
reg [27:0] baud_counter; |
reg [3:0] state; |
reg [7:0] lcl_data; |
reg calc_parity; |
|
initial o_uart = 1'b1; |
initial o_busy = 1'b1; |
initial state = `TXU_IDLE; |
// initial baud_counter = clocks_per_baud; |
always @(posedge i_clk) |
begin |
if (i_reset) |
begin |
baud_counter <= clocks_per_baud; |
o_uart <= 1'b1; |
o_busy <= 1'b1; |
state <= `TXU_IDLE; |
lcl_data <= 8'h0; |
calc_parity <= 1'b0; |
end else if (i_break) |
begin |
baud_counter <= break_condition; |
o_uart <= 1'b0; |
state <= `TXU_BREAK; |
calc_parity <= 1'b0; |
o_busy <= 1'b1; |
end else if (baud_counter != 0) |
begin // o_busy needs to be set coming into here |
baud_counter <= baud_counter - 28'h01; |
o_busy <= 1'b1; |
end else if (state == `TXU_BREAK) |
begin |
state <= `TXU_IDLE; |
o_busy <= 1'b1; |
o_uart <= 1'b1; |
calc_parity <= 1'b0; |
// Give us two stop bits before becoming available |
baud_counter <= clocks_per_baud<<2; |
end else if (state == `TXU_IDLE) // STATE_IDLE |
begin |
// baud_counter <= 0; |
r_setup <= i_setup; |
calc_parity <= 1'b0; |
if ((i_wr)&&(~o_busy)) |
begin // Immediately start us off with a start bit |
o_uart <= 1'b0; |
o_busy <= 1'b1; |
case(data_bits) |
2'b00: state <= `TXU_BIT_ZERO; |
2'b01: state <= `TXU_BIT_ONE; |
2'b10: state <= `TXU_BIT_TWO; |
2'b11: state <= `TXU_BIT_THREE; |
endcase |
lcl_data <= i_data; |
baud_counter <= clocks_per_baud-28'h01; |
end else begin // Stay in idle |
o_uart <= 1'b1; |
o_busy <= 0; |
// lcl_data is irrelevant |
// state <= state; |
end |
end else begin |
// One clock tick in each of these states ... |
baud_counter <= clocks_per_baud - 28'h01; |
o_busy <= 1'b1; |
if (state[3] == 0) // First 8 bits |
begin |
o_uart <= lcl_data[0]; |
calc_parity <= calc_parity ^ lcl_data[0]; |
if (state == `TXU_BIT_SEVEN) |
state <= (use_parity)?`TXU_PARITY:`TXU_STOP; |
else |
state <= state + 1; |
lcl_data <= { 1'b0, lcl_data[7:1] }; |
end else if (state == `TXU_PARITY) |
begin |
state <= `TXU_STOP; |
if (fixd_parity) |
o_uart <= parity_even; |
else |
o_uart <= calc_parity^((parity_even)? 1'b1:1'b0); |
end else if (state == `TXU_STOP) |
begin // two stop bit(s) |
o_uart <= 1'b1; |
if (dblstop) |
state <= `TXU_SECOND_STOP; |
else |
state <= `TXU_IDLE; |
calc_parity <= 1'b0; |
end else // `TXU_SECOND_STOP and default: |
begin |
state <= `TXU_IDLE; // Go back to idle |
o_uart <= 1'b1; |
// Still o_busy, since we need to wait |
// for the baud clock to finish counting |
// out this last bit. |
end |
end |
end |
|
endmodule |
|
|
|
|
/trunk/rtl/wbgpio.v
0,0 → 1,32
module wbgpio(i_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, o_wb_data, |
i_gpio, o_gpio, o_int); |
parameter NIN=16, NOUT=16, DEFAULT=16'h00; |
input i_clk; |
// |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input [31:0] i_wb_data; |
output wire [31:0] o_wb_data; |
// |
input [(NIN-1):0] i_gpio; |
output reg [(NOUT-1):0] o_gpio; |
// |
output reg o_int; |
|
// 9LUT's, 16 FF's |
always @(posedge i_clk) |
if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)) |
o_gpio <= ((o_gpio)&(~i_wb_data[(NOUT+16-1):16])) |
|((i_wb_data[(NOUT-1):0])&(i_wb_data[(NOUT+16-1):16])); |
|
reg [(NIN-1):0] x_gpio, r_gpio; |
// 3 LUTs, 33 FF's |
always @(posedge i_clk) |
begin |
x_gpio <= i_gpio; |
r_gpio <= x_gpio; |
o_int <= (x_gpio != r_gpio); |
end |
|
assign o_wb_data = { {(16-NIN){1'b0}}, r_gpio, |
{(16-NOUT){1'b0}}, o_gpio }; |
endmodule |
/trunk/rtl/toplevel.v
0,0 → 1,108
`timescale 10ns / 100ps |
module toplevel(i_clk_8mhz, |
o_qspi_cs_n, o_qspi_sck, io_qspi_dat, |
i_btn, o_led, o_pwm, o_pwm_shutdown_n, o_pwm_gain, |
i_uart, o_uart, |
i_kp_row, o_kp_col, |
i_gpio, o_gpio, |
io_scl, io_sda); |
input i_clk_8mhz; |
// |
// Quad SPI Flash |
output wire o_qspi_cs_n; |
output wire o_qspi_sck; |
inout wire [3:0] io_qspi_dat; |
// |
// General purpose I/O |
input [1:0] i_btn; |
output wire [3:0] o_led; |
output wire o_pwm, o_pwm_shutdown_n, o_pwm_gain; |
// |
// and our serial port |
input i_uart; |
output wire o_uart; |
// Our keypad |
input [3:0] i_kp_row; |
output wire [3:0] o_kp_col; |
// and our GPIO |
input [15:2] i_gpio; |
output wire [15:2] o_gpio; |
// and our I2C port |
inout io_scl, io_sda; |
|
///// |
wire ck_zero_0, clk_s; // intermediate_clk, intermediate_clk_n; |
|
// Clock frequency = (25 / 2) * 8Mhz |
// Clock period = 10 ns |
DCM_SP #( |
.CLKDV_DIVIDE(2.0), |
.CLKFX_DIVIDE(2), |
.CLKFX_MULTIPLY(20), |
.CLKIN_DIVIDE_BY_2("FALSE"), |
.CLKIN_PERIOD(125.0), |
.CLKOUT_PHASE_SHIFT("NONE"), |
.CLK_FEEDBACK("1X"), |
.DESKEW_ADJUST("SYSTEM_SYNCHRONOUS"), |
.DLL_FREQUENCY_MODE("LOW"), |
.DUTY_CYCLE_CORRECTION("TRUE"), |
.PHASE_SHIFT(0), |
.STARTUP_WAIT("TRUE") |
) u0( .CLKIN(i_clk_8mhz), |
.CLK0(ck_zero_0), |
.CLKFB(ck_zero_0), |
.CLKFX(clk_s), |
// .CLKFX180(intermediate_clk_n), |
.PSEN(1'b0), |
.RST(1'b0)); |
|
// Generate active-high reset. |
/* |
reg r_reset; |
initial r_reset = 1'b1; |
always @(posedge i_clk_12mhz) |
r_reset <= 1'b0; |
*/ |
assign reset_s = 1'b0; |
|
wire rx_stb, tx_stb; |
wire [7:0] rx_data, tx_data; |
wire tx_busy; |
wire [29:0] uart_setup; |
|
wire rx_break, rx_parity_err, rx_frame_err, rx_ck_uart, tx_break; |
assign tx_break = 1'b0; |
rxuart rcvuart(clk_s, reset_s, uart_setup, i_uart, rx_stb, rx_data, |
rx_break, rx_parity_err, rx_frame_err, rx_ck_uart); |
txuart tcvuart(clk_s, reset_s, uart_setup, tx_break, tx_stb, tx_data, |
o_uart, tx_busy); |
|
|
|
wire [3:0] qspi_dat; |
wire [1:0] qspi_bmod; |
wire [15:0] w_gpio; |
|
busmaster masterbus(clk_s, reset_s, |
// External ... bus control (if enabled) |
rx_stb, rx_data, tx_stb, tx_data, tx_busy, |
// SPI/SD-card flash |
o_qspi_cs_n, o_qspi_sck, qspi_dat, io_qspi_dat, qspi_bmod, |
// Board lights and switches |
i_btn, o_led, o_pwm, { o_pwm_shutdown_n, o_pwm_gain }, |
// Keypad connections |
i_kp_row, o_kp_col, |
// UART control |
uart_setup, |
// GPIO lines |
{ i_gpio, io_scl, io_sda }, w_gpio |
); |
|
assign io_qspi_dat = (~qspi_bmod[1])?({2'b11,1'bz,qspi_dat[0]}) |
:((qspi_bmod[0])?(4'bzzzz):(qspi_dat[3:0])); |
|
assign io_sda = (w_gpio[0]) ? 1'bz : 1'b0; |
assign io_scl = (w_gpio[1]) ? 1'bz : 1'b0; |
assign o_gpio[15:2] = w_gpio[15:2]; |
|
endmodule |
/trunk/rtl/builddate.v
0,0 → 1,108
`define DATESTAMP 32'h20151030 |
/trunk/rtl/wbicape6.v
0,0 → 1,304
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: wbicape6.v |
// |
// Project: Wishbone to ICAPE_SPARTAN6 interface conversion |
// |
// Purpose: This is a companion project to the ICAPE2 conversion, instead |
// involving a conversion from a 32-bit WISHBONE bus to read |
// and write the ICAPE_SPARTAN6 program. This is the 'non-simple' |
// portion of the interface, sporting all of the smarts necessary to run |
// the simple interface and make working with ICAPE as simple as |
// reading and writing from a wishbone bus. For example, register ID's |
// are given by bus addresses, even though they take extra cycles to |
// set and clear within the interface. |
// |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Following instructions on page 116-117 of the configuration guide ... |
// |
// W FFFF |
// W FFFF |
// W AA99 |
// W 5566 |
// W 2000 NOOP |
// W 2901 Write Type-1 packet header to read register 901?? |
// W 2000 NOOP |
// W 2000 NOOP |
// W 2000 NOOP |
// W 2000 NOOP |
// R vvvv Read register from previous packet header command |
// W 30A1 Write word to CMD register |
// W 000D DESYNC command |
// W 2000 NOOP |
// W 2000 NOOP |
// |
// Bits need to be bit-reversed within a byte |
// |
// |
// IPROG example |
// |
// W FFFF |
// W AA99 |
// W 5566 |
// W 3261 Write 1 words to GENERAL_1 |
// W xxxx write Multiboot start address [15:0] |
// W 3281 Write 1 words to GENERAL_2 |
// W xxxx write Opcode and multiboot start address [23:16] |
// W 32A1 Write 1 words to GENERAL_3 |
// W xxxx write Fallback start address |
// W 32C1 Write 1 words to GENERAL_4 |
// W xxxx write Opcode dne Fallback start address [23:16] |
// W 30A1 Write 1 word to CMD |
// W 000E IPGROG Cmd |
// W 2000 NOOP |
// |
// |
// |
// This fails when using wgregs on the XuLA2 board because the ICAPE port and |
// the JTAG port cannot both be active at the same time. |
// |
// |
`define ICAP_IDLE 5'h0 |
`define ICAP_START 5'h1 |
`define ICAP_CLOSE 5'hf |
module wbicape6(i_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, |
o_wb_ack, o_wb_stall, o_wb_data, dbg_data); |
input i_clk; |
// Wishbone slave inputs |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input [5:0] i_wb_addr; |
input [31:0] i_wb_data; |
// Wishbone outputs |
output reg o_wb_ack; |
output reg o_wb_stall; |
output wire [31:0] o_wb_data; |
output wire [31:0] dbg_data; |
|
// Interface to the lower level ICAPE port |
reg icap_cyc, icap_stb, icap_we; |
reg [15:0] icap_data_i; |
wire icap_ack, icap_stall; |
wire [15:0] icap_data_o; |
|
reg [4:0] state; |
reg r_we; |
reg [15:0] r_data; |
|
wire [25:0] icap_dbg; |
assign dbg_data = { i_wb_stb, state[3:0], r_we, icap_dbg }; |
|
reg stalled_state; |
reg [7:0] r_cmd_word; |
wire [15:0] w_cmd_word; |
assign w_cmd_word = { 3'b001, r_cmd_word, 5'h00 }; // Type-1 packet hdr |
initial icap_stb = 1'b0; |
initial icap_cyc = 1'b0; |
initial state = `ICAP_IDLE; |
always @(posedge i_clk) |
begin |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b0; |
if (stalled_state) |
state <= `ICAP_IDLE; |
else if ((~icap_stall)&&(state != `ICAP_IDLE)) |
state <= state + 1; |
case(state) |
`ICAP_IDLE: begin |
icap_stb <= 1'b0; |
icap_cyc <= 1'b0; |
state <= `ICAP_IDLE; |
r_data <= i_wb_data[15:0]; |
r_we <= i_wb_we; |
if ((i_wb_cyc)&&(i_wb_stb)) |
begin |
state <= `ICAP_START; |
icap_stb <= i_wb_stb; |
icap_cyc <= i_wb_cyc; |
icap_we <= 1'b1; |
icap_data_i <= 16'hffff; |
r_cmd_word <= { (i_wb_we)? 2'b10:2'b01, i_wb_addr }; |
o_wb_stall <= 1'b1; |
end end |
`ICAP_START: begin |
if (~icap_stall) |
icap_data_i <= 16'hffff; |
end |
5'h2: begin |
if (~icap_stall) |
icap_data_i <= 16'haa99; |
end |
5'h3: begin |
if (~icap_stall) |
icap_data_i <= 16'h5566; |
end |
5'h4: begin |
if (~icap_stall) |
icap_data_i <= 16'h2000; |
end |
5'h5: begin |
if (~icap_stall) |
begin |
icap_data_i <= w_cmd_word; // Includes address |
end end |
5'h6: begin // Write |
if (~icap_stall) |
begin |
if (r_we) |
icap_data_i <= r_data; |
else |
icap_data_i <= 16'h2000; |
end end |
5'h7: begin |
// Need to send four NOOPs before we can begin |
// reading. Send the four NOOPs for a write anyway. |
if (~icap_stall) |
icap_data_i <= 16'h2000; |
end |
5'h8: begin |
if (~icap_stall) |
icap_data_i <= 16'h2000; |
end |
5'h9: begin |
if (~icap_stall) |
begin |
icap_data_i <= 16'h2000; |
if (r_we) |
state <= `ICAP_CLOSE; |
end end |
5'ha: begin |
if (~icap_stall) |
begin |
// We now request the chip enable line be |
// dropped, so we can switch from writing to |
// reading |
icap_data_i <= 16'h2000; |
icap_stb <= 1'b0; |
icap_cyc <= 1'b0; |
end end |
5'hb: begin |
if (~icap_stall) |
begin |
// Switch the write line to read, must be done |
// w/the chip enable off (hence _cyc=0). O/w |
// the interface will abort. |
icap_data_i <= 16'h2000; |
icap_we <=1'b0; |
end end |
5'hc: begin |
if (~icap_stall) |
begin |
// We can finally issue our read command |
// Re-activate the interface, and read |
icap_data_i <= 16'h2000; |
icap_stb <= 1'b1; |
icap_cyc <= 1'b1; |
end end |
5'hd: begin |
if (~icap_stall) |
begin |
// De-activate the interface again so we can |
// switch back to write. |
icap_data_i <= 16'h2000; |
icap_stb <= 1'b0; |
icap_cyc <= 1'b0; |
end end |
5'he: begin |
if (~icap_stall) |
begin |
// Switch back to write while the interface |
// is deactivated. |
icap_we <= 1'b1; |
icap_data_i <= 16'h2000; |
end end |
`ICAP_CLOSE: begin |
if (~icap_stall) |
begin |
icap_we <= 1'b1; |
// Type 1: Write 1 word to CMD register |
icap_data_i <= 16'h30a1; |
r_data <= icap_data_o; |
icap_stb <= 1'b1; |
icap_cyc <= 1'b1; |
end end |
5'h10: begin // DESYNC Command |
if (~icap_stall) |
begin |
icap_data_i <= 16'h000d; |
end end |
5'h11: begin // DESYNC must be followed by two NOOPs |
if (~icap_stall) |
begin |
icap_data_i <= 16'h2000; |
end end |
5'h12: begin // NOOP |
if (~icap_stall) |
begin |
icap_data_i <= 16'h2000; |
state <= `ICAP_IDLE; |
o_wb_ack <= 1'b1; |
o_wb_stall <= 1'b0; |
end end |
default: begin |
// If we were in the middle of a bus cycle, and got |
// here then ... we just failed that cycle. Setting |
// the bus error flag would be appropriate, but we |
// have no such flag to our interface. Hence we just |
// drop things and depend upon a bus watchdog to |
// catch that we aren't answering. |
o_wb_ack <= 1'b0; |
o_wb_stall <= 1'b0; |
icap_stb <= 1'b0; |
icap_cyc <= 1'b0; |
state <= `ICAP_IDLE; |
end |
endcase |
end |
|
wbicapesimple spartancfg(i_clk, icap_cyc, icap_stb, icap_we, |
icap_data_i, |
icap_ack, icap_stall, icap_data_o, |
icap_dbg); |
|
|
assign o_wb_data = { 16'h0000, r_data }; |
|
reg [4:0] last_state; |
initial last_state = `ICAP_IDLE; |
always @(posedge i_clk) |
last_state <= state; |
|
reg [11:0] reset_ctr; |
always @(posedge i_clk) |
if (last_state != state) |
reset_ctr <= 0; |
else if (state == `ICAP_IDLE) |
reset_ctr <= 0; |
else |
reset_ctr <= reset_ctr + 1; |
always @(posedge i_clk) |
stalled_state <= (&reset_ctr); |
|
endmodule |
/trunk/rtl/wbscope.v
0,0 → 1,333
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: wbscope.v |
// |
// Project: FPGA Library of Routines |
// |
// Purpose: This is a generic/library routine for providing a bus accessed |
// 'scope' or (perhaps more appropriately) a bus accessed logic |
// analyzer. The general operation is such that this 'scope' can |
// record and report on any 32 bit value transiting through the |
// FPGA. Once started and reset, the scope records a copy of the |
// input data every time the clock ticks with the circuit enabled. |
// That is, it records these values up until the trigger. Once |
// the trigger goes high, the scope will record for bw_holdoff |
// more counts before stopping. Values may then be read from the |
// buffer, oldest to most recent. After reading, the scope may |
// then be reset for another run. |
// |
// In general, therefore, operation happens in this fashion: |
// 1. A reset is issued. |
// 2. Recording starts, in a circular buffer, and continues until |
// 3. The trigger line is asserted. |
// The scope registers the asserted trigger by setting |
// the 'o_triggered' output flag. |
// 4. A counter then ticks until the last value is written |
// The scope registers that it has stopped recording by |
// setting the 'o_stopped' output flag. |
// 5. The scope recording is then paused until the next reset. |
// 6. While stopped, the CPU can read the data from the scope |
// 7. -- oldest to most recent |
// 8. -- one value per i_rd&i_clk |
// 9. Writes to the data register reset the address to the |
// beginning of the buffer |
// |
// Although the data width DW is parameterized, it is not very changable, |
// since the width is tied to the width of the data bus, as is the |
// control word. Therefore changing the data width would require changing |
// the interface. It's doable, but it would be a change to the interface. |
// |
// The SYNCHRONOUS parameter turns on and off meta-stability |
// synchronization. Ideally a wishbone scope able to handle one or two |
// clocks would have a changing number of ports as this SYNCHRONOUS |
// parameter changed. Other than running another script to modify |
// this, I don't know how to do that so ... we'll just leave it running |
// off of two clocks or not. |
// |
// |
// Internal to this routine, registers and wires are named with one of the |
// following prefixes: |
// |
// i_ An input port to the routine |
// o_ An output port of the routine |
// br_ A register, controlled by the bus clock |
// dr_ A register, controlled by the data clock |
// bw_ A wire/net, controlled by the bus clock |
// dw_ A wire/net, controlled by the data clock |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
///////////////////////////////////////////////////////////////////////////// |
module wbscope(i_clk, i_ce, i_trigger, i_data, |
i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, |
o_wb_ack, o_wb_stall, o_wb_data, |
o_interrupt); |
parameter LGMEM = 5'd10, BUSW = 32, SYNCHRONOUS=1; |
// The input signals that we wish to record |
input i_clk, i_ce, i_trigger; |
input [(BUSW-1):0] i_data; |
// The WISHBONE bus for reading and configuring this scope |
input i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we; |
input i_wb_addr; // One address line only |
input [(BUSW-1):0] i_wb_data; |
output wire o_wb_ack, o_wb_stall; |
output reg [(BUSW-1):0] o_wb_data; |
// And, finally, for a final flair --- offer to interrupt the CPU after |
// our trigger has gone off. This line is equivalent to the scope |
// being stopped. It is not maskable here. |
output wire o_interrupt; |
|
reg [(LGMEM-1):0] raddr; |
reg [(BUSW-1):0] mem[0:((1<<LGMEM)-1)]; |
|
// Our status/config register |
wire bw_reset_request, bw_manual_trigger, |
bw_disable_trigger, bw_reset_complete; |
reg [22:0] br_config; |
wire [19:0] bw_holdoff; |
initial br_config = ((1<<(LGMEM-1))-4); |
always @(posedge i_wb_clk) |
if ((i_wb_cyc)&&(i_wb_stb)&&(~i_wb_addr)) |
begin |
if (i_wb_we) |
br_config <= { i_wb_data[31], |
(i_wb_data[27]), |
i_wb_data[26], |
i_wb_data[19:0] }; |
end else if (bw_reset_complete) |
br_config[22] <= 1'b1; |
assign bw_reset_request = (~br_config[22]); |
assign bw_manual_trigger = (br_config[21]); |
assign bw_disable_trigger = (br_config[20]); |
assign bw_holdoff = br_config[19:0]; |
|
wire dw_reset, dw_manual_trigger, dw_disable_trigger; |
generate |
if (SYNCHRONOUS > 0) |
begin |
assign dw_reset = bw_reset_request; |
assign dw_manual_trigger = bw_manual_trigger; |
assign dw_disable_trigger = bw_disable_trigger; |
assign bw_reset_complete = bw_reset_request; |
end else begin |
reg r_reset_complete; |
reg [2:0] r_iflags, q_iflags; |
|
// Resets are synchronous to the bus clock, not the data clock |
// so do a clock transfer here |
initial q_iflags = 3'b000; |
initial r_reset_complete = 1'b0; |
always @(posedge i_clk) |
begin |
q_iflags <= { bw_reset_request, bw_manual_trigger, bw_disable_trigger }; |
r_iflags <= q_iflags; |
r_reset_complete <= (dw_reset); |
end |
|
assign dw_reset = r_iflags[2]; |
assign dw_manual_trigger = r_iflags[1]; |
assign dw_disable_trigger = r_iflags[0]; |
|
reg q_reset_complete, qq_reset_complete; |
// Pass an acknowledgement back from the data clock to the bus |
// clock that the reset has been accomplished |
initial q_reset_complete = 1'b0; |
initial qq_reset_complete = 1'b0; |
always @(posedge i_wb_clk) |
begin |
q_reset_complete <= r_reset_complete; |
qq_reset_complete <= q_reset_complete; |
end |
|
assign bw_reset_complete = qq_reset_complete; |
end endgenerate |
|
// |
// Set up the trigger |
// |
// |
// Write with the i-clk, or input clock. All outputs read with the |
// WISHBONE-clk, or i_wb_clk clock. |
reg dr_triggered, dr_primed; |
wire dw_trigger; |
assign dw_trigger = (dr_primed)&&( |
((i_trigger)&&(~dw_disable_trigger)) |
||(dr_triggered) |
||(dw_manual_trigger)); |
initial dr_triggered = 1'b0; |
always @(posedge i_clk) |
if (dw_reset) |
dr_triggered <= 1'b0; |
else if ((i_ce)&&(dw_trigger)) |
dr_triggered <= 1'b1; |
|
// |
// Determine when memory is full and capture is complete |
// |
// Writes take place on the data clock |
reg dr_stopped; |
reg [19:0] counter; // This is unsigned |
initial dr_stopped = 1'b0; |
initial counter = 20'h0000; |
always @(posedge i_clk) |
if (dw_reset) |
begin |
counter <= 0; |
dr_stopped <= 1'b0; |
end else if ((i_ce)&&(dr_triggered)) |
begin // MUST BE a < and not <=, so that we can keep this w/in |
// 20 bits. Else we'd need to add a bit to comparison |
// here. |
if (counter < bw_holdoff) |
counter <= counter + 20'h01; |
else |
dr_stopped <= 1'b1; |
end |
|
// |
// Actually do our writes to memory. Record, via 'primed' when |
// the memory is full. |
// |
// The 'waddr' address that we are using really crosses two clock |
// domains. While writing and changing, it's in the data clock |
// domain. Once stopped, it becomes part of the bus clock domain. |
// The clock transfer on the stopped line handles the clock |
// transfer for these signals. |
// |
reg [(LGMEM-1):0] waddr; |
initial waddr = {(LGMEM){1'b0}}; |
initial dr_primed = 1'b0; |
always @(posedge i_clk) |
if (dw_reset) // For simulation purposes, supply a valid value |
begin |
waddr <= 0; // upon reset. |
dr_primed <= 1'b0; |
end else if ((i_ce)&&((~dr_triggered)||(counter < bw_holdoff))) |
begin |
// mem[waddr] <= i_data; |
waddr <= waddr + {{(LGMEM-1){1'b0}},1'b1}; |
dr_primed <= (dr_primed)||(&waddr); |
end |
always @(posedge i_clk) |
if ((i_ce)&&((~dr_triggered)||(counter < bw_holdoff))) |
mem[waddr] <= i_data; |
|
// |
// Clock transfer of the status signals |
// |
wire bw_stopped, bw_triggered, bw_primed; |
generate |
if (SYNCHRONOUS > 0) |
begin |
assign bw_stopped = dr_stopped; |
assign bw_triggered = dr_triggered; |
assign bw_primed = dr_primed; |
end else begin |
// These aren't a problem, since none of these are strobe |
// signals. They goes from low to high, and then stays high |
// for many clocks. Swapping is thus easy--two flip flops to |
// protect against meta-stability and we're done. |
// |
reg [2:0] q_oflags, r_oflags; |
initial q_oflags = 3'h0; |
initial r_oflags = 3'h0; |
always @(posedge i_wb_clk) |
if (bw_reset_request) |
begin |
q_oflags <= 3'h0; |
r_oflags <= 3'h0; |
end else begin |
q_oflags <= { dr_stopped, dr_triggered, dr_primed }; |
r_oflags <= q_oflags; |
end |
|
assign bw_stopped = r_oflags[2]; |
assign bw_triggered = r_oflags[1]; |
assign bw_primed = r_oflags[0]; |
end endgenerate |
|
// Reads use the bus clock |
reg br_wb_ack; |
initial br_wb_ack = 1'b0; |
wire bw_cyc_stb; |
assign bw_cyc_stb = ((i_wb_cyc)&&(i_wb_stb)); |
always @(posedge i_wb_clk) |
begin |
if ((bw_reset_request) |
||((bw_cyc_stb)&&(i_wb_addr)&&(i_wb_we))) |
raddr <= 0; |
else if ((bw_cyc_stb)&&(i_wb_addr)&&(~i_wb_we)&&(bw_stopped)) |
raddr <= raddr + {{(LGMEM-1){1'b0}},1'b1}; // Data read, when stopped |
|
if ((bw_cyc_stb)&&(~i_wb_we)) |
begin // Read from the bus |
br_wb_ack <= 1'b1; |
end else if ((bw_cyc_stb)&&(i_wb_we)) |
// We did this write above |
br_wb_ack <= 1'b1; |
else // Do nothing if either i_wb_cyc or i_wb_stb are low |
br_wb_ack <= 1'b0; |
end |
|
reg [31:0] nxt_mem; |
always @(posedge i_wb_clk) |
nxt_mem <= mem[raddr+waddr+ |
(((bw_cyc_stb)&&(i_wb_addr)&&(~i_wb_we)) ? |
{{(LGMEM-1){1'b0}},1'b1} : { (LGMEM){1'b0}} )]; |
|
wire [4:0] bw_lgmem; |
assign bw_lgmem = LGMEM; |
always @(posedge i_wb_clk) |
if (~i_wb_addr) // Control register read |
o_wb_data <= { bw_reset_request, |
bw_stopped, |
bw_triggered, |
bw_primed, |
bw_manual_trigger, |
bw_disable_trigger, |
(raddr == {(LGMEM){1'b0}}), |
bw_lgmem, |
bw_holdoff }; |
else if (~bw_stopped) // read, prior to stopping |
o_wb_data <= i_data; |
else // if (i_wb_addr) // Read from FIFO memory |
o_wb_data <= nxt_mem; // mem[raddr+waddr]; |
|
assign o_wb_stall = 1'b0; |
assign o_wb_ack = (i_wb_cyc)&&(br_wb_ack); |
|
reg br_level_interrupt; |
initial br_level_interrupt = 1'b0; |
assign o_interrupt = (bw_stopped)&&(~bw_disable_trigger) |
&&(~br_level_interrupt); |
always @(posedge i_wb_clk) |
if ((bw_reset_complete)||(bw_reset_request)) |
br_level_interrupt<= 1'b0; |
else |
br_level_interrupt<= (bw_stopped)&&(~bw_disable_trigger); |
|
endmodule |
/trunk/rtl/llqspi.v
0,0 → 1,395
/////////////////////////////////////////////////////////////////////////// |
// |
// Filename: llqspi.v |
// |
// Project: Wishbone Controlled Quad SPI Flash Controller |
// |
// Purpose: Reads/writes a word (user selectable number of bytes) of data |
// to/from a Quad SPI port. The port is understood to be |
// a normal SPI port unless the driver requests four bit mode. |
// When not in use, unlike our previous SPI work, no bits will |
// toggle. |
// |
// Creator: Dan Gisselquist |
// Gisselquist Technology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): 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. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
`define QSPI_IDLE 3'h0 |
`define QSPI_START 3'h1 |
`define QSPI_BITS 3'h2 |
`define QSPI_READY 3'h3 |
`define QSPI_HOLDING 3'h4 |
`define QSPI_STOP 3'h5 |
`define QSPI_STOP_B 3'h6 |
|
// Modes |
`define QSPI_MOD_SPI 2'b00 |
`define QSPI_MOD_QOUT 2'b10 |
`define QSPI_MOD_QIN 2'b11 |
|
module llqspi(i_clk, |
// Module interface |
i_wr, i_hold, i_word, i_len, i_spd, i_dir, |
o_word, o_valid, o_busy, |
// QSPI interface |
o_sck, o_cs_n, o_mod, o_dat, i_dat); |
input i_clk; |
// Chip interface |
// Can send info |
// i_dir = 1, i_spd = 0, i_hold = 0, i_wr = 1, |
// i_word = { 1'b0, 32'info to send }, |
// i_len = # of bytes in word-1 |
input i_wr, i_hold; |
input [31:0] i_word; |
input [1:0] i_len; // 0=>8bits, 1=>16 bits, 2=>24 bits, 3=>32 bits |
input i_spd; // 0 -> normal QPI, 1 -> QSPI |
input i_dir; // 0 -> read, 1 -> write to SPI |
output reg [31:0] o_word; |
output reg o_valid, o_busy; |
// Interface with the QSPI lines |
output reg o_sck; |
output reg o_cs_n; |
output reg [1:0] o_mod; |
output reg [3:0] o_dat; |
input [3:0] i_dat; |
|
// output wire [22:0] o_dbg; |
// assign o_dbg = { state, spi_len, |
// o_busy, o_valid, o_cs_n, o_sck, o_mod, o_dat, i_dat }; |
|
// Timing: |
// |
// Tick Clk BSY/WR CS_n BIT/MO STATE |
// 0 1 0/0 1 - |
// 1 1 0/1 1 - |
// 2 1 1/0 0 - QSPI_START |
// 3 0 1/0 0 - QSPI_START |
// 4 0 1/0 0 0 QSPI_BITS |
// 5 1 1/0 0 0 QSPI_BITS |
// 6 0 1/0 0 1 QSPI_BITS |
// 7 1 1/0 0 1 QSPI_BITS |
// 8 0 1/0 0 2 QSPI_BITS |
// 9 1 1/0 0 2 QSPI_BITS |
// 10 0 1/0 0 3 QSPI_BITS |
// 11 1 1/0 0 3 QSPI_BITS |
// 12 0 1/0 0 4 QSPI_BITS |
// 13 1 1/0 0 4 QSPI_BITS |
// 14 0 1/0 0 5 QSPI_BITS |
// 15 1 1/0 0 5 QSPI_BITS |
// 16 0 1/0 0 6 QSPI_BITS |
// 17 1 1/1 0 6 QSPI_BITS |
// 18 0 1/1 0 7 QSPI_READY |
// 19 1 0/1 0 7 QSPI_READY |
// 20 0 1/0/V 0 8 QSPI_BITS |
// 21 1 1/0 0 8 QSPI_BITS |
// 22 0 1/0 0 9 QSPI_BITS |
// 23 1 1/0 0 9 QSPI_BITS |
// 24 0 1/0 0 10 QSPI_BITS |
// 25 1 1/0 0 10 QSPI_BITS |
// 26 0 1/0 0 11 QSPI_BITS |
// 27 1 1/0 0 11 QSPI_BITS |
// 28 0 1/0 0 12 QSPI_BITS |
// 29 1 1/0 0 12 QSPI_BITS |
// 30 0 1/0 0 13 QSPI_BITS |
// 31 1 1/0 0 13 QSPI_BITS |
// 32 0 1/0 0 14 QSPI_BITS |
// 33 1 1/0 0 14 QSPI_BITS |
// 34 0 1/0 0 15 QSPI_READY |
// 35 1 1/0 0 15 QSPI_READY |
// 36 1 1/0/V 0 - QSPI_STOP |
// 37 1 1/0 0 - QSPI_STOPB |
// 38 1 1/0 1 - QSPI_IDLE |
// 39 1 0/0 1 - |
// Now, let's switch from single bit to quad mode |
// 40 1 0/0 1 - QSPI_IDLE |
// 41 1 0/1 1 - QSPI_IDLE |
// 42 1 1/0 0 - QSPI_START |
// 43 0 1/0 0 - QSPI_START |
// 44 0 1/0 0 0 QSPI_BITS |
// 45 1 1/0 0 0 QSPI_BITS |
// 46 0 1/0 0 1 QSPI_BITS |
// 47 1 1/0 0 1 QSPI_BITS |
// 48 0 1/0 0 2 QSPI_BITS |
// 49 1 1/0 0 2 QSPI_BITS |
// 50 0 1/0 0 3 QSPI_BITS |
// 51 1 1/0 0 3 QSPI_BITS |
// 52 0 1/0 0 4 QSPI_BITS |
// 53 1 1/0 0 4 QSPI_BITS |
// 54 0 1/0 0 5 QSPI_BITS |
// 55 1 1/0 0 5 QSPI_BITS |
// 56 0 1/0 0 6 QSPI_BITS |
// 57 1 1/1/QR 0 6 QSPI_BITS |
// 58 0 1/1/QR 0 7 QSPI_READY |
// 59 1 0/1/QR 0 7 QSPI_READY |
// 60 0 1/0/?/V 0 8-11 QSPI_BITS |
// 61 1 1/0/? 0 8-11 QSPI_BITS |
// 62 0 1/0/? 0 12-15 QSPI_BITS |
// 63 1 1/0/? 0 12-15 QSPI_BITS |
// 64 1 1/0/?/V 0 - QSPI_STOP |
// 65 1 1/0/? 0 - QSPI_STOPB |
// 66 1 1/0/? 1 - QSPI_IDLE |
// 67 1 0/0 1 - QSPI_IDLE |
// Now let's try something entirely in Quad read mode, from the |
// beginning |
// 68 1 0/1/QR 1 - QSPI_IDLE |
// 69 1 1/0 0 - QSPI_START |
// 70 0 1/0 0 - QSPI_START |
// 71 0 1/0 0 0-3 QSPI_BITS |
// 72 1 1/0 0 0-3 QSPI_BITS |
// 73 0 1/1/QR 0 4-7 QSPI_BITS |
// 74 1 0/1/QR 0 4-7 QSPI_BITS |
// 75 0 1/?/?/V 0 8-11 QSPI_BITS |
// 76 1 1/?/? 0 8-11 QSPI_BITS |
// 77 0 1/1/QR 0 12-15 QSPI_BITS |
// 78 1 0/1/QR 0 12-15 QSPI_BITS |
// 79 0 1/?/?/V 0 16-19 QSPI_BITS |
// 80 1 1/0 0 16-19 QSPI_BITS |
// 81 0 1/0 0 20-23 QSPI_BITS |
// 82 1 1/0 0 20-23 QSPI_BITS |
// 83 1 1/0/V 0 - QSPI_STOP |
// 84 1 1/0 0 - QSPI_STOPB |
// 85 1 1/0 1 - QSPI_IDLE |
// 86 1 0/0 1 - QSPI_IDLE |
|
wire i_miso; |
assign i_miso = i_dat[1]; |
|
reg r_spd, r_dir; |
reg [5:0] spi_len; |
reg [31:0] r_word; |
reg [30:0] r_input; |
reg [2:0] state; |
initial state = `QSPI_IDLE; |
initial o_sck = 1'b1; |
initial o_cs_n = 1'b1; |
initial o_dat = 4'hd; |
initial o_valid = 1'b0; |
initial o_busy = 1'b0; |
initial r_input = 31'h000; |
always @(posedge i_clk) |
if ((state == `QSPI_IDLE)&&(o_sck)) |
begin |
o_cs_n <= 1'b1; |
o_valid <= 1'b0; |
o_busy <= 1'b0; |
o_mod <= `QSPI_MOD_SPI; |
if (i_wr) |
begin |
r_word <= i_word; |
state <= `QSPI_START; |
r_spd <= i_spd; |
r_dir <= i_dir; |
spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8; |
o_cs_n <= 1'b0; |
o_busy <= 1'b1; |
o_sck <= 1'b1; |
end |
end else if (state == `QSPI_START) |
begin // We come in here with sck high, stay here 'til sck is low |
o_sck <= 1'b0; |
if (o_sck == 1'b0) |
begin |
state <= `QSPI_BITS; |
spi_len<= spi_len - ( (r_spd)? 6'h4 : 6'h1 ); |
if (r_spd) |
r_word <= { r_word[27:0], 4'h0 }; |
else |
r_word <= { r_word[30:0], 1'b0 }; |
end |
o_mod <= (r_spd) ? { 1'b1, r_dir } : `QSPI_MOD_SPI; |
o_cs_n <= 1'b0; |
o_busy <= 1'b1; |
o_valid <= 1'b0; |
if (r_spd) |
begin |
o_dat <= r_word[31:28]; |
// r_word <= { r_word[27:0], 4'h0 }; |
end else begin |
o_dat <= { 3'b110, r_word[31] }; |
// r_word <= { r_word[30:0], 1'b0 }; |
end |
end else if (~o_sck) |
begin |
o_sck <= 1'b1; |
o_busy <= ((state != `QSPI_READY)||(~i_wr)); |
o_valid <= 1'b0; |
end else if (state == `QSPI_BITS) |
begin |
// Should enter into here with at least a spi_len |
// of one, perhaps more |
o_sck <= 1'b0; |
o_busy <= 1'b1; |
if (r_spd) |
begin |
o_dat <= r_word[31:28]; |
r_word <= { r_word[27:0], 4'h0 }; |
spi_len <= spi_len - 6'h4; |
if (spi_len == 6'h4) |
state <= `QSPI_READY; |
end else begin |
o_dat <= { 3'b110, r_word[31] }; |
r_word <= { r_word[30:0], 1'b0 }; |
spi_len <= spi_len - 6'h1; |
if (spi_len == 6'h1) |
state <= `QSPI_READY; |
end |
|
o_valid <= 1'b0; |
if (~o_mod[1]) |
r_input <= { r_input[29:0], i_miso }; |
else if (o_mod[1]) |
r_input <= { r_input[26:0], i_dat }; |
end else if (state == `QSPI_READY) |
begin |
o_valid <= 1'b0; |
o_cs_n <= 1'b0; |
o_busy <= 1'b1; |
// This is the state on the last clock (both low and |
// high clocks) of the data. Data is valid during |
// this state. Here we chose to either STOP or |
// continue and transmit more. |
o_sck <= (i_hold); // No clocks while holding |
if((~o_busy)&&(i_wr))// Acknowledge a new request |
begin |
state <= `QSPI_BITS; |
o_busy <= 1'b1; |
o_sck <= 1'b0; |
|
// Read the new request off the bus |
r_spd <= i_spd; |
r_dir <= i_dir; |
// Set up the first bits on the bus |
o_mod <= (i_spd) ? { 1'b1, i_dir } : `QSPI_MOD_SPI; |
if (i_spd) |
begin |
o_dat <= i_word[31:28]; |
r_word <= { i_word[27:0], 4'h0 }; |
// spi_len <= spi_len - 4; |
spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8 |
- 6'h4; |
end else begin |
o_dat <= { 3'b110, i_word[31] }; |
r_word <= { i_word[30:0], 1'b0 }; |
spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8 |
- 6'h1; |
end |
|
// Read a bit upon any transition |
o_valid <= 1'b1; |
if (~o_mod[1]) |
begin |
r_input <= { r_input[29:0], i_miso }; |
o_word <= { r_input[30:0], i_miso }; |
end else if (o_mod[1]) |
begin |
r_input <= { r_input[26:0], i_dat }; |
o_word <= { r_input[27:0], i_dat }; |
end |
end else begin |
o_sck <= 1'b1; |
state <= (i_hold)?`QSPI_HOLDING : `QSPI_STOP; |
o_busy <= (~i_hold); |
|
// Read a bit upon any transition |
o_valid <= 1'b1; |
if (~o_mod[1]) |
begin |
r_input <= { r_input[29:0], i_miso }; |
o_word <= { r_input[30:0], i_miso }; |
end else if (o_mod[1]) |
begin |
r_input <= { r_input[26:0], i_dat }; |
o_word <= { r_input[27:0], i_dat }; |
end |
end |
end else if (state == `QSPI_HOLDING) |
begin |
// We need this state so that the o_valid signal |
// can get strobed with our last result. Otherwise |
// we could just sit in READY waiting for a new command. |
// |
// Incidentally, the change producing this state was |
// the result of a nasty race condition. See the |
// commends in wbqspiflash for more details. |
// |
o_valid <= 1'b0; |
o_cs_n <= 1'b0; |
o_busy <= 1'b0; |
if((~o_busy)&&(i_wr))// Acknowledge a new request |
begin |
state <= `QSPI_BITS; |
o_busy <= 1'b1; |
o_sck <= 1'b0; |
|
// Read the new request off the bus |
r_spd <= i_spd; |
r_dir <= i_dir; |
// Set up the first bits on the bus |
o_mod<=(i_spd)?{ 1'b1, i_dir } : `QSPI_MOD_SPI; |
if (i_spd) |
begin |
o_dat <= i_word[31:28]; |
r_word <= { i_word[27:0], 4'h0 }; |
spi_len<= { 1'b0, i_len, 3'b100 }; |
end else begin |
o_dat <= { 3'b110, i_word[31] }; |
r_word <= { i_word[30:0], 1'b0 }; |
spi_len<= { 1'b0, i_len, 3'b111 }; |
end |
end else begin |
o_sck <= 1'b1; |
state <= (i_hold)?`QSPI_HOLDING : `QSPI_STOP; |
o_busy <= (~i_hold); |
end |
end else if (state == `QSPI_STOP) |
begin |
o_sck <= 1'b1; // Stop the clock |
o_valid <= 1'b0; // Output may have just been valid, but no more |
o_busy <= 1'b1; // Still busy till port is clear |
state <= `QSPI_STOP_B; |
o_mod <= `QSPI_MOD_SPI; |
end else if (state == `QSPI_STOP_B) |
begin |
o_cs_n <= 1'b1; |
o_sck <= 1'b1; |
// Do I need this???? |
// spi_len <= 3; // Minimum CS high time before next cmd |
state <= `QSPI_IDLE; |
o_valid <= 1'b0; |
o_busy <= 1'b1; |
o_mod <= `QSPI_MOD_SPI; |
end else begin // Invalid states, should never get here |
state <= `QSPI_STOP; |
o_valid <= 1'b0; |
o_busy <= 1'b1; |
o_cs_n <= 1'b1; |
o_sck <= 1'b1; |
o_mod <= `QSPI_MOD_SPI; |
o_dat <= 4'hd; |
end |
|
endmodule |
|
/trunk/rtl/busmaster.v
0,0 → 1,424
// |
// |
// Filename: busmaster.v |
// |
// Project: FPGA library development (S6 development board) |
// |
// Purpose: |
// |
// Creator: Dan Gisselquist |
// Gisselquist Technology, LLC |
// |
// Copyright: 2015 |
// |
// |
`include "builddate.v" |
// |
`define NO_ZIP_WBU_DELAY |
`define INCLUDE_ZIPPY |
`define IMPLEMENT_ONCHIP_RAM |
`ifndef VERILATOR |
`define FANCY_ICAP_ACCESS |
`endif |
`define FLASH_ACCESS |
`define CFG_SCOPE |
`define INCLUDE_RTC // 2017 slice LUTs w/o, 2108 with (!!!) |
module busmaster(i_clk, i_rst, |
i_rx_stb, i_rx_data, o_tx_stb, o_tx_data, i_tx_busy, |
// The SPI Flash lines |
o_qspi_cs_n, o_qspi_sck, o_qspi_dat, i_qspi_dat, o_qspi_mod, |
// The board I/O |
i_btn, o_led, o_pwm, o_pwm_aux, |
// Keypad connections |
i_kp_row, o_kp_col, |
// UART control |
o_uart_setup, |
// GPIO lines |
i_gpio, o_gpio); |
parameter ZIP_ADDRESS_WIDTH=23, ZA=ZIP_ADDRESS_WIDTH, |
CMOD_ZIPCPU_RESET_ADDRESS=23'h400100, |
BUS_ADDRESS_WIDTH=23, BAW=23; // 24bits->2,258,23b->2181 |
input i_clk, i_rst; |
// The bus commander, via an external JTAG port |
input i_rx_stb; |
input [7:0] i_rx_data; |
output reg o_tx_stb; |
output reg [7:0] o_tx_data; |
input i_tx_busy; |
// SPI flash control |
output wire o_qspi_cs_n, o_qspi_sck; |
output wire [3:0] o_qspi_dat; |
input [3:0] i_qspi_dat; |
output wire [1:0] o_qspi_mod; |
// Board I/O |
input [1:0] i_btn; |
output wire [3:0] o_led; |
output wire o_pwm; |
output wire [1:0] o_pwm_aux; |
// Keypad |
input [3:0] i_kp_row; |
output wire [3:0] o_kp_col; |
// UART control |
output wire [29:0] o_uart_setup; |
// GPIO liines |
input [15:0] i_gpio; |
output wire [15:0] o_gpio; |
|
|
// |
// |
// Master wishbone wires |
// |
// |
wire wb_cyc, wb_stb, wb_we, wb_stall, wb_ack, wb_err; |
wire [31:0] wb_data, wb_idata; |
wire [(BAW-1):0] wb_addr; |
wire [5:0] io_addr; |
assign io_addr = { |
wb_addr[22], // Flash |
wb_addr[13], // RAM |
wb_addr[11], // RTC |
wb_addr[10], // CFG |
wb_addr[ 9], // SCOPE |
wb_addr[ 8] }; // I/O |
|
// Wires going to devices |
// And then headed back home |
wire w_interrupt; |
// Oh, and the debug control for the ZIP CPU |
wire zip_dbg_ack, zip_dbg_stall; |
wire [31:0] zip_dbg_data; |
|
|
// |
// |
// The BUS master (source): The ZipCPU |
// |
// |
wire zip_cyc, zip_stb, zip_we, zip_cpu_int; |
wire [(ZA-1):0] w_zip_addr; |
wire [(BAW-1):0] zip_addr; |
wire [31:0] zip_data; |
// and then coming from devices |
wire zip_ack, zip_stall, zip_err; |
wire dwb_we, dwb_stb, dwb_cyc, dwb_ack, dwb_stall, dwb_err; |
wire [(BAW-1):0] dwb_addr; |
wire [31:0] dwb_odata; |
|
// wire [31:0] zip_debug; |
// |
// We'll define our RESET_ADDRESS to be halfway through our flash memory. |
// `define CMOD_ZIPCPU_RESET_ADDRESS 23'h600000 |
// |
// Ahm, No. We can actually do much better than that. Our toplevel *.bit file |
// only takes up only 335kB. Let's give it some room to grow to 1024 kB. Then |
// 23 can start our ROM at 23'h400100 |
// |
// Not so fast. In hindsight, we really want to be able to adjust the load and |
// the program separately. So, instead, let's place our RESET address at the |
// second flash erase block. That way, we can change our program code found |
// in the flash without needing to change our FPGA load and vice versa. |
// |
// 23'h404000 |
zipbones #(CMOD_ZIPCPU_RESET_ADDRESS,ZA,6) |
thecpu(i_clk, 1'b0, |
// Zippys wishbone interface |
wb_cyc, wb_stb, wb_we, w_zip_addr, wb_data, |
wb_ack, wb_stall, wb_idata, wb_err, |
w_interrupt, zip_cpu_int, |
// Debug wishbone interface |
1'b0, 1'b0,1'b0, 1'b0, 32'h00, |
zip_dbg_ack, zip_dbg_stall, zip_dbg_data); |
generate |
if (ZA < BAW) |
assign wb_addr = { {(BAW-ZA){1'b0}}, w_zip_addr }; |
else |
assign wb_addr = w_zip_addr; |
endgenerate |
|
wire io_sel, flash_sel, flctl_sel, scop_sel, cfg_sel, mem_sel, |
rtc_sel, none_sel, many_sel; |
wire flash_ack, scop_ack, cfg_ack, mem_ack; |
wire rtc_ack, rtc_stall; |
`ifdef INCLUDE_RTC |
assign rtc_stall = 1'b0; |
`endif |
wire io_stall, flash_stall, scop_stall, cfg_stall, mem_stall; |
reg io_ack, uart_ack; |
|
wire [31:0] flash_data, scop_data, cfg_data, mem_data, pwm_data, |
spio_data, gpio_data, uart_data; |
reg [31:0] io_data; |
reg [(BAW-1):0] bus_err_addr; |
|
assign wb_ack = (wb_cyc)&&((io_ack)||(scop_ack)||(cfg_ack) |
||(uart_ack) |
`ifdef INCLUDE_RTC |
||(rtc_ack) |
`endif |
||(mem_ack)||(flash_ack)||((none_sel)&&(1'b1))); |
assign wb_stall = ((io_sel)&&(io_stall)) |
||((scop_sel)&&(scop_stall)) |
||((cfg_sel)&&(cfg_stall)) |
||((mem_sel)&&(mem_stall)) |
`ifdef INCLUDE_RTC |
||((rtc_sel)&&(rtc_stall)) |
`endif |
||((flash_sel||flctl_sel)&&(flash_stall)); |
// (none_sel)&&(1'b0) |
|
/* |
assign wb_idata = (io_ack)?io_data |
: ((scop_ack)?scop_data |
: ((cfg_ack)?cfg_data |
: ((mem_ack)?mem_data |
: ((flash_ack)?flash_data |
: 32'h00)))); |
*/ |
assign wb_idata = (io_ack|scop_ack)?((io_ack )? io_data : scop_data) |
: ((cfg_ack|uart_ack) ? ((cfg_ack)?cfg_data: uart_data) |
: ((mem_ack|rtc_ack)?((mem_ack)?mem_data:rtc_data) |
: flash_data)); // if (flash_ack) |
assign wb_err = ((wb_cyc)&&(wb_stb)&&(none_sel || many_sel)) || many_ack; |
|
// Addresses ... |
// 0000 xxxx configuration/control registers |
// 1 xxxx xxxx xxxx xxxx xxxx Up-sampler taps |
assign io_sel =((wb_cyc)&&(io_addr[5:0]==6'h1)); |
assign flctl_sel= 1'b0; // ((wb_cyc)&&(io_addr[5:1]==5'h1)); |
assign scop_sel =((wb_cyc)&&(io_addr[5:1]==5'h1)); |
assign cfg_sel =((wb_cyc)&&(io_addr[5:2]==4'h1)); |
// zip_sel is not on the bus at this point |
`ifdef INCLUDE_RTC |
assign rtc_sel =((wb_cyc)&&(io_addr[5:3]==3'h1)); |
`endif |
assign mem_sel =((wb_cyc)&&(io_addr[5:4]==2'h1)); |
assign flash_sel=((wb_cyc)&&(io_addr[5])); |
|
assign none_sel =((wb_cyc)&&(wb_stb)&&(io_addr==6'h0)); |
/* |
assign many_sel =((wb_cyc)&&(wb_stb)&&( |
{3'h0, io_sel} |
+{3'h0, flctl_sel} |
// +{3'h0, scop_sel} |
+{3'h0, cfg_sel} |
+{3'h0, mem_sel} |
+{3'h0, flash_sel} > 1)); |
*/ |
assign many_sel = 1'b0; |
|
wire many_ack; |
assign many_ack =((wb_cyc)&&( |
{3'h0, io_ack} |
+{3'h0, scop_ack} |
+{3'h0, cfg_ack} |
`ifdef INCLUDE_RTC |
+{3'h0, rtc_ack} |
`endif |
+{3'h0, mem_ack} |
+{3'h0, flash_ack} > 1)); |
|
wire flash_interrupt, scop_interrupt, tmra_int, tmrb_int, |
rtc_interrupt, gpio_int, pwm_int, keypad_int,button_int; |
|
|
// |
// |
// |
reg rx_rdy; |
wire [10:0] int_vector; |
assign int_vector = { gpio_int, pwm_int, keypad_int, |
~i_tx_busy, rx_rdy, tmrb_int, tmra_int, |
rtc_interrupt, scop_interrupt, |
wb_err, button_int }; |
|
wire [31:0] pic_data; |
icontrol #(11) pic(i_clk, 1'b0, |
(wb_cyc)&&(wb_stb)&&(io_sel) |
&&(wb_addr[3:0]==4'h0)&&(wb_we), |
wb_data, pic_data, int_vector, w_interrupt); |
|
initial bus_err_addr = `DATESTAMP; |
always @(posedge i_clk) |
if (wb_err) |
bus_err_addr <= wb_addr; |
|
wire zta_ack, zta_stall, ztb_ack, ztb_stall; |
wire [31:0] timer_a, timer_b; |
ziptimer zipt_a(i_clk, 1'b0, 1'b1, wb_cyc, |
(wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h2), |
wb_we, wb_data, zta_ack, zta_stall, timer_a, |
tmra_int); |
ziptimer zipt_b(i_clk, 1'b0, 1'b1, wb_cyc, |
(wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h3), |
wb_we, wb_data, ztb_ack, ztb_stall, timer_b, |
tmrb_int); |
|
wire [31:0] rtc_data; |
`ifdef INCLUDE_RTC |
wire rtcd_ack, rtcd_stall, ppd; |
// rtcdate thedate(i_clk, ppd, wb_cyc, (wb_stb)&&(io_sel), wb_we, |
// wb_data, rtcd_ack, rtcd_stall, date_data); |
reg r_rtc_ack; |
initial r_rtc_ack = 1'b0; |
always @(posedge i_clk) |
r_rtc_ack <= ((wb_stb)&&(rtc_sel)); |
assign rtc_ack = r_rtc_ack; |
|
rtclight |
#(32'h35afe5) // 80 MHz clock |
thetime(i_clk, wb_cyc, |
((wb_stb)&&(rtc_sel)), wb_we, |
{ 1'b0, wb_addr[1:0] }, wb_data, rtc_data, |
rtc_interrupt, ppd); |
`else |
assign rtc_interrupt = 1'b0; |
assign rtc_data = 32'h00; |
assign rtc_ack = 1'b0; |
`endif |
|
always @(posedge i_clk) |
case(wb_addr[3:0]) |
4'h0: io_data <= pic_data; |
4'h1: io_data <= { {(32-BAW){1'b0}}, bus_err_addr }; |
4'h2: io_data <= timer_a; |
4'h3: io_data <= timer_b; |
4'h4: io_data <= pwm_data; |
4'h5: io_data <= spio_data; |
4'h6: io_data <= gpio_data; |
4'h7: io_data <= uart_data; |
default: io_data <= `DATESTAMP; |
// 4'h8: io_data <= `DATESTAMP; |
endcase |
always @(posedge i_clk) |
io_ack <= (wb_cyc)&&(wb_stb)&&(io_sel); |
assign io_stall = 1'b0; |
|
wire pwm_ack, pwm_stall; |
wbpwmaudio theaudio(i_clk, wb_cyc, |
((wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h4)), wb_we, |
1'b0, wb_data, |
pwm_ack, pwm_stall, pwm_data, o_pwm, o_pwm_aux, |
pwm_int); |
|
// |
// Special Purpose I/O: Keypad, button, LED status and control |
// |
spio thespio(i_clk, wb_cyc,(wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h5),wb_we, |
wb_data, spio_data, o_kp_col, i_kp_row, i_btn, o_led, |
keypad_int, button_int); |
|
// |
// General purpose (sort of) I/O: (Bottom two bits robbed in each |
// direction for an I2C link at the toplevel.v design) |
// |
wbgpio #(16,16,16'hffff) thegpio(i_clk, wb_cyc, |
(wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h6), wb_we, |
wb_data, gpio_data, i_gpio, o_gpio, gpio_int); |
|
// |
// |
// Rudimentary serial port control |
// |
reg [7:0] r_rx_data; |
// Baud rate is set by clock rate / baud rate. |
// Thus, 80MHz / 115200MBau |
// = 694.4, or about 0x2b6. |
// although the CPU might struggle to keep up at this speed without a |
// hardware buffer. |
// |
// We'll add the flag for two stop bits. |
assign o_uart_setup = 30'h080002b6; // 115200 MBaud @ an 80MHz clock |
|
initial o_tx_stb = 1'b0; |
initial o_tx_data = 8'h00; |
always @(posedge i_clk) |
if ((wb_cyc)&&(wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h7)&&(wb_we)) |
begin |
o_tx_data <= wb_data[7:0]; |
o_tx_stb <= 1'b1; |
end |
else if ((o_tx_stb)&&(~i_tx_busy)) |
o_tx_stb <= 1'b0; |
initial rx_rdy = 1'b0; |
always @(posedge i_clk) |
if (i_rx_stb) |
r_rx_data <= i_rx_data; |
always @(posedge i_clk) |
begin |
if((wb_cyc)&&(wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h7)&&(~wb_we)) |
rx_rdy <= i_rx_stb; |
else if (i_rx_stb) |
rx_rdy <= (rx_rdy | i_rx_stb); |
end |
assign uart_data = { 23'h0, ~rx_rdy, r_rx_data }; |
always @(posedge i_clk) |
uart_ack<= ((wb_cyc)&&(wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h7)); |
|
|
|
// |
// FLASH MEMORY CONFIGURATION ACCESS |
// |
wire flash_cs_n, flash_sck, flash_mosi; |
wbqspiflash #(24) flashmem(i_clk, |
wb_cyc,(wb_stb&&flash_sel),(wb_stb)&&(flctl_sel),wb_we, |
wb_addr[21:0], wb_data, |
flash_ack, flash_stall, flash_data, |
o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat, |
flash_interrupt); |
|
// |
// MULTIBOOT/ICAPE2 CONFIGURATION ACCESS |
// |
wire [31:0] cfg_scope; |
`ifdef FANCY_ICAP_ACCESS |
wbicape6 fpga_cfg(i_clk, wb_cyc,(cfg_sel)&&(wb_stb), wb_we, |
wb_addr[5:0], wb_data, |
cfg_ack, cfg_stall, cfg_data, |
cfg_scope); |
`else |
reg r_cfg_ack; |
always @(posedge i_clk) |
r_cfg_ack <= (wb_cyc)&&(cfg_sel)&&(wb_stb); |
assign cfg_ack = r_cfg_ack; |
assign cfg_stall = 1'b0; |
assign cfg_data = 32'h00; |
assign cfg_scope = 32'h00; |
`endif |
|
|
// |
// ON-CHIP RAM MEMORY ACCESS |
// |
memdev #(12) ram(i_clk, wb_cyc, (wb_stb)&&(mem_sel), wb_we, |
wb_addr[11:0], wb_data, mem_ack, mem_stall, mem_data); |
|
// |
// |
// WISHBONE SCOPE |
// |
// |
// |
// |
wire [31:0] scop_cfg_data; |
wire scop_cfg_ack, scop_cfg_stall, scop_cfg_interrupt; |
`ifdef CFG_SCOPE |
wire scop_cfg_trigger; |
assign scop_cfg_trigger = (wb_cyc)&&(wb_stb)&&(cfg_sel); |
wbscope #(5'ha) wbcfgscope(i_clk, 1'b1, scop_cfg_trigger, cfg_scope, |
// Wishbone interface |
i_clk, wb_cyc, ((wb_stb)&&(scop_sel)&&(wb_addr[2:1]==2'b01)), |
wb_we, wb_addr[0], wb_data, |
scop_cfg_ack, scop_cfg_stall, scop_cfg_data, |
scop_cfg_interrupt); |
`endif |
|
assign scop_interrupt = scop_cfg_interrupt; |
assign scop_ack = scop_cfg_ack; |
assign scop_stall = scop_cfg_stall; |
assign scop_data = scop_cfg_data; |
|
endmodule |
|
// 0x8684 interrupts ...??? |
/trunk/rtl/spio.v
0,0 → 1,47
module spio(i_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, o_wb_data, |
o_kp_col, i_kp_row, i_btn, o_led, |
o_kp_int, o_btn_int); |
// |
input i_clk; |
// |
input i_wb_cyc, i_wb_stb, i_wb_we; |
input [31:0] i_wb_data; |
output wire [31:0] o_wb_data; |
// |
output reg [3:0] o_kp_col; |
input [3:0] i_kp_row; |
input [1:0] i_btn; |
output reg [3:0] o_led; |
output reg o_kp_int, o_btn_int; |
|
initial o_kp_col = 4'h0; |
initial o_led = 4'h0; |
always @(posedge i_clk) |
if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)) |
begin |
o_kp_col <= ((o_kp_col)&(~i_wb_data[11:8])) |
|((i_wb_data[15:12])&(i_wb_data[11:8])); |
// o_led <= ((o_led)&(~i_wb_data[7:4])) |
// |((i_wb_data[3:0])&(i_wb_data[7:4])); |
o_led[0] <= (i_wb_data[4])?i_wb_data[0]:o_led[0]; |
o_led[1] <= (i_wb_data[5])?i_wb_data[1]:o_led[1]; |
o_led[2] <= (i_wb_data[6])?i_wb_data[2]:o_led[2]; |
o_led[3] <= (i_wb_data[7])?i_wb_data[3]:o_led[3]; |
end |
|
reg [3:0] x_kp_row, r_kp_row; |
reg [1:0] x_btn, r_btn; |
|
always @(posedge i_clk) |
begin |
x_kp_row <= i_kp_row; |
x_btn <= i_btn; |
r_kp_row <= x_kp_row; |
r_btn <= x_btn; |
o_kp_int <= ~(&r_kp_row); |
o_btn_int <= |(r_btn); |
end |
|
assign o_wb_data = { 16'h00, o_kp_col, r_kp_row, 2'b00, r_btn, o_led }; |
|
endmodule |
/trunk/mkdatev.pl
0,0 → 1,9
#!/usr/bin/perl |
|
$now = time; |
($sc,$mn,$nhr,$ndy,$nmo,$nyr,$nwday,$nyday,$nisdst) = localtime($now); |
$nyr = $nyr+1900; $nmo = $nmo+1; |
|
print "`define DATESTAMP 32\'h"; |
printf("%04d%02d%02d\n", $nyr, $nmo, $ndy); |
|
trunk/mkdatev.pl
Property changes :
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: trunk/Makefile
===================================================================
--- trunk/Makefile (nonexistent)
+++ trunk/Makefile (revision 2)
@@ -0,0 +1,31 @@
+.PHONY: all
+all: datestamp archive
+# BENCH := `find bench -name Makefile` `find bench -name "*.cpp"` `find bench -name "*.h"`
+BENCH :=
+RTL := `find rtl -name "*.v"` `find rtl -name Makefile`
+NOTES := `find . -name "*.txt"` `find . -name "*.html"`
+SW := `find sw -name "*.cpp"` `find sw -name "*.h"` \
+ `find sw -name "*.sh"` `find sw -name "*.py"` \
+ `find sw -name "*.pl"` `find sw -name Makefile`
+# PROJ := xilinx/xula.prj xilinx/xula.xise xilinx/xula.xst \
+# xilinx/xula.ut xilinx/Makefile
+PROJ :=
+BIN := `find xilinx -name "*.bit"`
+CONSTRAINTS := cmod.ucf
+YYMMDD := `date +%Y%m%d`
+
+datestamp: $(YYMMDD)-build.v
+$(YYMMDD)-build.v:
+ -rm -rf 2*-build.v
+ perl mkdatev.pl > $(YYMMDD)-build.v
+ cd rtl; ln -fs ../$(YYMMDD)-build.v builddate.v
+
+.PHONY: archive
+archive:
+ tar --transform s,^,$(YYMMDD)-s6/, -chjf $(YYMMDD)-xula.tjz $(BENCH) $(SW) $(RTL) $(NOTES) $(PROJ) $(BIN) $(CONSTRAINTS)
+
+# .PHONY: bit
+# bit:
+# cd xilinx ; make xula.bit
+
+
Index: trunk/cmod.ucf
===================================================================
--- trunk/cmod.ucf (nonexistent)
+++ trunk/cmod.ucf (revision 2)
@@ -0,0 +1,140 @@
+#FPGA_GCLK
+NET "i_clk_8mhz" LOC = "N8" | IOSTANDARD = LVCMOS33;
+NET "i_clk_8mhz" TNM_NET = "i_clk_8mhz";
+TIMESPEC "TSi_clk_8mhz" = PERIOD "i_clk_8mhz" 125.0 ns HIGH 50%;
+
+#CLK_LFC
+# NET "i_clk_pps" LOC = "N7" | IOSTANDARD = LVCMOS33;
+
+#BTNs
+NET "i_btn<0>" LOC = "P8" | IOSTANDARD = LVCMOS33;
+NET "i_btn<1>" LOC = "P9" | IOSTANDARD = LVCMOS33;
+
+#LEDs
+NET "o_led<0>" LOC = "N3" | IOSTANDARD = LVCMOS33;
+NET "o_led<1>" LOC = "P3" | IOSTANDARD = LVCMOS33;
+NET "o_led<2>" LOC = "N4" | IOSTANDARD = LVCMOS33;
+NET "o_led<3>" LOC = "P4" | IOSTANDARD = LVCMOS33;
+
+# Flash
+NET "o_qspi_sck" LOC="N13" | IOSTANDARD = LVCMOS33;
+NET "o_qspi_cs_n" LOC="P2" | IOSTANDARD = LVCMOS33;
+NET "io_qspi_dat<0>" LOC="P11" | IOSTANDARD = LVCMOS33;
+NET "io_qspi_dat<1>" LOC="N11" | IOSTANDARD = LVCMOS33;
+NET "io_qspi_dat<2>" LOC="N10" | IOSTANDARD = LVCMOS33;
+NET "io_qspi_dat<3>" LOC="P10" | IOSTANDARD = LVCMOS33;
+
+#DEPP Signals
+# NET "DEPP_WAIT" LOC = "B6" | IOSTANDARD = LVCMOS33;
+# NET "DEPP_ASTB" LOC = "A6" | IOSTANDARD = LVCMOS33;
+# NET "DEPP_DSTB" LOC = "B7" | IOSTANDARD = LVCMOS33;
+# NET "DEPP_WRITE" LOC = "A7" | IOSTANDARD = LVCMOS33;
+# NET "DBUS<0>" LOC = "B9" | IOSTANDARD = LVCMOS33;
+# NET "DBUS<1>" LOC = "A9" | IOSTANDARD = LVCMOS33;
+# NET "DBUS<2>" LOC = "B10" | IOSTANDARD = LVCMOS33;
+# NET "DBUS<3>" LOC = "A10" | IOSTANDARD = LVCMOS33;
+# NET "DBUS<4>" LOC = "B11" | IOSTANDARD = LVCMOS33;
+# NET "DBUS<5>" LOC = "A11" | IOSTANDARD = LVCMOS33;
+# NET "DBUS<6>" LOC = "B12" | IOSTANDARD = LVCMOS33;
+# NET "DBUS<7>" LOC = "A12" | IOSTANDARD = LVCMOS33;
+
+#IO PORTs
+
+# #A
+NET "i_uart" LOC = "P5" | IOSTANDARD = LVCMOS33;
+NET "o_uart" LOC = "N5" | IOSTANDARD = LVCMOS33;
+NET "o_pwm" LOC = "P12" | IOSTANDARD = LVCMOS33;
+NET "io_scl" LOC = "N6" | IOSTANDARD = LVCMOS33 | PULLUP; # io_scl
+NET "io_sda" LOC = "P7" | IOSTANDARD = LVCMOS33 | PULLUP; # io_sda
+NET "o_pwm_shutdown_n" LOC = "K1" | IOSTANDARD = LVCMOS33 | PULLUP;
+NET "o_pwm_gain" LOC = "K2" | IOSTANDARD = LVCMOS33 | PULLUP;
+
+#
+NET "o_gpio<2>" LOC = "N12" | IOSTANDARD = LVCMOS33; # o_mosi
+NET "o_gpio<3>" LOC = "L14" | IOSTANDARD = LVCMOS33; # o_sck
+NET "o_gpio<4>" LOC = "L13" | IOSTANDARD = LVCMOS33; # o_ss
+NET "o_gpio<5>" LOC = "K14" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<6>" LOC = "K13" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<7>" LOC = "J14" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<8>" LOC = "J13" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<9>" LOC = "H14" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<10>" LOC = "H13" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<11>" LOC = "F14" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<12>" LOC = "F13" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<13>" LOC = "G14" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<14>" LOC = "G13" | IOSTANDARD = LVCMOS33;
+NET "o_gpio<15>" LOC = "E14" | IOSTANDARD = LVCMOS33;
+
+# NET "i_gpio<0>" LOC = "A3" | IOSTANDARD = LVCMOS33;
+# NET "i_gpio<1>" LOC = "B3" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<2>" LOC = "A2" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<3>" LOC = "B1" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<4>" LOC = "C1" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<5>" LOC = "D1" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<6>" LOC = "D2" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<7>" LOC = "E1" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<8>" LOC = "E2" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<9>" LOC = "F1" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<10>" LOC = "F2" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<11>" LOC = "H1" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<12>" LOC = "H2" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<13>" LOC = "G1" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<14>" LOC = "G2" | IOSTANDARD = LVCMOS33;
+NET "i_gpio<15>" LOC = "J1" | IOSTANDARD = LVCMOS33;
+
+# NET "PORTA<0>" LOC = "P5" | IOSTANDARD = LVCMOS33 | PULLUP; # i_uart
+# NET "PORTA<1>" LOC = "N5" | IOSTANDARD = LVCMOS33 | PULLUP; # o_uart
+# NET "PORTA<2>" LOC = "N6" | IOSTANDARD = LVCMOS33 | PULLUP; # io_scl
+# NET "PORTA<3>" LOC = "P7" | IOSTANDARD = LVCMOS33 | PULLUP; # io_sda
+# NET "PORTA<4>" LOC = "P12" | IOSTANDARD = LVCMOS33 | PULLUP; # o_pwm
+# NET "PORTA<5>" LOC = "N12" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTA<6>" LOC = "L14" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTA<7>" LOC = "L13" | IOSTANDARD = LVCMOS33 | PULLUP;
+#
+# #B Input ports
+# NET "PORTB<0>" LOC = "K14" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTB<1>" LOC = "K13" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTB<2>" LOC = "J14" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTB<3>" LOC = "J13" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTB<4>" LOC = "H14" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTB<5>" LOC = "H13" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTB<6>" LOC = "F14" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTB<7>" LOC = "F13" | IOSTANDARD = LVCMOS33 | PULLUP;
+#
+# #C Input ports
+# NET "PORTC<0>" LOC = "G14" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTC<1>" LOC = "G13" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTC<2>" LOC = "E14" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTC<3>" LOC = "E13" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTC<4>" LOC = "D14" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTC<5>" LOC = "D13" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTC<6>" LOC = "C13" | IOSTANDARD = LVCMOS33 | PULLUP;
+#
+# #D Output ports
+# NET "PORTD<0>" LOC = "A3" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTD<1>" LOC = "B3" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTD<2>" LOC = "A2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTD<3>" LOC = "B1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTD<4>" LOC = "C1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTD<5>" LOC = "D1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTD<6>" LOC = "D2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTD<7>" LOC = "E1" | IOSTANDARD = LVCMOS33 | PULLUP;
+#
+# #E Output ports
+# NET "PORTE<0>" LOC = "E2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTE<1>" LOC = "F1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTE<2>" LOC = "F2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTE<3>" LOC = "H1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTE<4>" LOC = "H2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTE<5>" LOC = "G1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTE<6>" LOC = "G2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTE<7>" LOC = "J1" | IOSTANDARD = LVCMOS33 | PULLUP;
+#
+# #F Unused ports
+# NET "PORTF<0>" LOC = "J2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTF<1>" LOC = "K1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTF<2>" LOC = "K2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTF<3>" LOC = "L1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTF<4>" LOC = "L2" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTF<5>" LOC = "M1" | IOSTANDARD = LVCMOS33 | PULLUP;
+# NET "PORTF<6>" LOC = "M2" | IOSTANDARD = LVCMOS33 | PULLUP;