Skip to content

Commit

Permalink
plumb sd card image into sdcard_model.vhdl #766
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Gardner-Stephen committed Mar 3, 2024
1 parent 65e60c8 commit a4db91b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/vhdl/sdcard_model.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ entity sdcard_model is
cs_bo : in std_logic;
sclk_o : in std_logic;
mosi_o : in std_logic;
miso_i : out std_logic
miso_i : out std_logic;

flash_address : out unsigned(47 downto 0);
flash_rdata : in unsigned(7 downto 0)
);

end entity;
Expand Down Expand Up @@ -42,9 +45,7 @@ architecture cheap_imitation of sdcard_model is
signal sdcard_idle : std_logic := '1';

signal block_size : integer range 0 to 4096 := 512;
signal flash_address : unsigned(47 downto 0) := to_unsigned(0,48);
signal flash_byte : unsigned(7 downto 0) := x"99";
signal flash_rdata : unsigned(7 downto 0) := x"42";
signal bits_remaining : integer range 0 to 8 := 0;
signal bytes_remaining : integer range 0 to 4096 := 0;

Expand Down
33 changes: 31 additions & 2 deletions src/vhdl/tb_sdcard.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ architecture test_arch of tb_sdcard is
type sector_list_t is array(0 to (sector_slot_count-1)) of integer;
signal sector_slots : sector_slot_buffer_t := (others => (others => x"00"));
signal sector_numbers : sector_list_t := (others => 999999999);
signal sector_count : integer := 0;
signal sector_count : integer := 1; -- slot 0 holds the dummy "all zeroes" sector

signal flash_address : unsigned(47 downto 0);
signal last_flash_address : unsigned(47 downto 0);
signal flash_rdata : unsigned(7 downto 0);
signal flash_slot : integer := 0;

begin

Expand All @@ -56,7 +61,10 @@ begin
cs_bo => cs_bo,
sclk_o => sclk_o,
mosi_o => mosi_o,
miso_i => miso_i
miso_i => miso_i,

flash_address => flash_address,
flash_rdata => flash_rdata
);

sdcard_controller0: entity work.sdcardio
Expand Down Expand Up @@ -139,7 +147,28 @@ begin
variable v : unsigned(15 downto 0);

procedure clock_tick is
variable slot_num : integer := 0;
variable sector_found : boolean := false;
begin

-- Simulate SD card flash memory
if flash_address(47 downto 9) /= last_flash_address(47 downto 9) then
flash_slot <= 0;
for i in 1 to (sector_count-1) loop
if to_integer(flash_address(47 downto 9)) = sector_numbers(i) then
flash_slot <= i;
report "SDCARDIMG: Sector $" & to_hexstring(flash_address(47 downto 9)) & " maps to sector slot " & integer'image(i);
sector_found := true;
exit;
end if;
end loop;
if sector_found = false then
report "SDCARDIMG: Sector $" & to_hexstring(flash_address(47 downto 9)) & " maps to an empty sector.";
end if;
flash_rdata <= sector_slots(flash_slot)(to_integer(flash_address(8 downto 0)));
end if;
last_flash_address <= flash_address;

clock162 <= not clock162;
if clock162 = '1' then
pixelclock <= not pixelclock;
Expand Down

0 comments on commit a4db91b

Please sign in to comment.