Skip to content

Commit

Permalink
writing to any line on the disk
Browse files Browse the repository at this point in the history
  • Loading branch information
710lucas committed Jul 30, 2024
1 parent eae06a7 commit 0adcf20
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
15 changes: 14 additions & 1 deletion Examples/TestingDISK
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
//Adding 0xF6 to register 0
//0xF6 is the instruction to Write to disk
ADD 0xFFF6 0XFF00 0x0000

//Debug: printing value in register 9
PRNT 0x0000 0x0000 0x0000

WBUS 0xff0A 0xff00 0x0000
//Writing the instruction 0xF6 (stored in register 0) to the bus
//Writing the data that will be written: 0x0A
//writing the address (line) in which the data will be written: 0x01 (line 2)
//Remember, the first line is 0
WBUS 0xff0A 0xff01 0x0000

//Writing the instruction 0xF6 (stored in register 0) to the bus
//Writing the data that will be written: 0x01
//writing the address (line) in which the data will be written: 0x09 (line 10)
WBUS 0xFF01 0xFF09 0x0000

//Stopping the program
STP 0x0000 0x0000 0x0000
Binary file modified Examples/TestingDISK.bin
Binary file not shown.
49 changes: 45 additions & 4 deletions src/emulator/IO/disk/disk.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "./disk.hpp"
#include <iostream>
#include <fstream>
#include <vector>

/*
Disclaimer
Expand All @@ -16,12 +17,52 @@ Disk::Disk(SystemBus &bus) : IOInterface(bus){

void Disk::write(byte data){

const long lineNumber = this->getBus()->readAddress();


/*
To be able to add a byte to any line we need to
read the file so we can edit the line that we want
and then rewrite the file with the new data
We can't edit a specific line with std::ofstream
*/

//Preparing to read file
std::ifstream readingFile("disk.txt");
std::vector<std::string> lines;
std::string line;

//Reading file
if(readingFile.is_open()){
while(std::getline(readingFile, line)){
lines.push_back(line);
}
readingFile.close();
}

//Adding empty lines if the file is too short
while(lines.size() <= lineNumber){
lines.push_back("");
}

//Editing the line
lines[lineNumber] = std::to_string(static_cast<int>(data));


std::ofstream file("disk.txt");

if(file.is_open()){
file << static_cast<int>(data) << '\n';
file.close();
} else {
//Rewriting to file
std::ofstream writingFile("disk.txt");

if(writingFile.is_open()){
for(const auto& line: lines){
writingFile << line << "\n";
}
writingFile.close();
}

else {
std::cout << "Unable to open file\n";
}

Expand Down

0 comments on commit 0adcf20

Please sign in to comment.