Route D (real fixes in embedded open-source projects) keeps producing the hard tasks: of 64 measured so far, 15 are HARD and 4 MEDIUM, mostly because Qwen exhausts its context in the larger real workspaces. This adds CANopen SRDO and ASCII timing, FreeRTOS DHCP, lwIP MQTT fragments, printf extremal exponents, Zephyr packet buffers and heatshrink (HARD), plus lwjson, RFM9x, SFUD and nanopb proto3 (MEDIUM). From batch 27 (spec-dense implementation tasks) come one HARD and one MEDIUM; that batch is winding down after 30 tasks gave 1 HARD and 3 MEDIUM. Each task comes from the commit Qwen measured, its digest matches, and it is still present at the batch branch head. Agent timeouts stay excluded: a pyserial attempt timed out on Qwen's own blocking read yet graded PASS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
181 lines
7.0 KiB
C
181 lines
7.0 KiB
C
#include "virtual_flash.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static uint32_t address(const uint8_t *p, size_t n) {
|
|
uint32_t a = 0;
|
|
while (n--) a = (a << 8) | *p++;
|
|
return a;
|
|
}
|
|
|
|
static int protected_at(const virtual_flash *v, uint32_t a) {
|
|
unsigned bp = (v->status >> 2) & 15;
|
|
unsigned blocks = v->capacity / 65536;
|
|
unsigned block = a / 65536;
|
|
if (!bp) return 0;
|
|
if (v->mf != 0xc2 || bp == 7 || bp == 8 || bp == 15) return 1;
|
|
if (bp < 7) return block >= blocks - (1u << (bp - 1));
|
|
return block < blocks - (1u << (14 - bp));
|
|
}
|
|
|
|
static sfud_err bus(const sfud_spi *spi, const uint8_t *w, size_t wn, uint8_t *r, size_t rn) {
|
|
virtual_flash *v = spi->user_data;
|
|
uint32_t a;
|
|
size_t n = v->four_byte ? 4 : 3, i;
|
|
if (!wn) return SFUD_ERR_WRITE;
|
|
switch (w[0]) {
|
|
case 0x9f:
|
|
if (rn == 3) {r[0] = v->mf; r[1] = v->type; r[2] = v->density;}
|
|
return SFUD_SUCCESS;
|
|
case 0x5a:
|
|
if (rn) memset(r, 0xff, rn);
|
|
return SFUD_SUCCESS;
|
|
case 0x66: return SFUD_SUCCESS;
|
|
case 0x99:
|
|
v->four_byte = 0; v->volatile_enable = 0; v->aai = 0;
|
|
v->status &= ~3u;
|
|
return SFUD_SUCCESS;
|
|
case 0x05:
|
|
if (rn) memset(r, v->status, rn);
|
|
return SFUD_SUCCESS;
|
|
case 0x06:
|
|
if (v->fault == 1) return SFUD_ERR_WRITE;
|
|
v->status |= 2;
|
|
return SFUD_SUCCESS;
|
|
case 0x04:
|
|
v->status &= ~2u; v->aai = 0;
|
|
return SFUD_SUCCESS;
|
|
case 0x50:
|
|
/* This opcode is provided only by the SST status-register profile. */
|
|
if (v->mf == 0xbf) v->volatile_enable = 1;
|
|
return SFUD_SUCCESS;
|
|
case 0x01:
|
|
if (v->fault == 2) return SFUD_ERR_WRITE;
|
|
if (wn == 2 && ((v->status & 2) || v->volatile_enable)) {
|
|
v->status = w[1] & 0xfc;
|
|
v->volatile_enable = 0;
|
|
}
|
|
return SFUD_SUCCESS;
|
|
case 0xb7:
|
|
v->four_byte = 1; return SFUD_SUCCESS;
|
|
case 0xe9:
|
|
v->four_byte = 0; return SFUD_SUCCESS;
|
|
case 0x03:
|
|
case 0x13:
|
|
if (w[0] == 0x13) n = 4;
|
|
if (wn != n + 1) break;
|
|
a = address(w + 1, n);
|
|
if ((uint64_t)a + rn > v->capacity) return SFUD_ERR_READ;
|
|
if (rn) memcpy(r, v->memory + a, rn);
|
|
return SFUD_SUCCESS;
|
|
case 0x02:
|
|
if (wn <= n + 1) break;
|
|
a = address(w + 1, n);
|
|
if ((uint64_t)a + wn - n - 1 > v->capacity) return SFUD_ERR_WRITE;
|
|
if (!(v->status & 2) || protected_at(v, a)) return SFUD_SUCCESS;
|
|
for (i = n + 1; i < wn; i++)
|
|
v->memory[(a & ~255u) | ((a + i - n - 1) & 255u)] &= w[i];
|
|
v->status &= ~2u;
|
|
return SFUD_SUCCESS;
|
|
case 0xad:
|
|
if (wn == n + 3) {a = address(w + 1, n); i = n + 1;}
|
|
else if (wn == 3 && v->aai) {a = v->aai_next; i = 1;}
|
|
else break;
|
|
if ((uint64_t)a + 2 > v->capacity) return SFUD_ERR_WRITE;
|
|
if (!(v->status & 2) || protected_at(v, a)) return SFUD_SUCCESS;
|
|
v->memory[a] &= w[i]; v->memory[a+1] &= w[i+1];
|
|
v->aai = 1; v->aai_next = a + 2;
|
|
return SFUD_SUCCESS;
|
|
case 0x20:
|
|
case 0xd8:
|
|
if (wn != n + 1) break;
|
|
a = address(w + 1, n);
|
|
if (a >= v->capacity) return SFUD_ERR_WRITE;
|
|
if (!(v->status & 2) || protected_at(v, a)) return SFUD_SUCCESS;
|
|
i = w[0] == 0x20 ? 4096 : 65536;
|
|
memset(v->memory + (a & ~(i-1)), 0xff, i);
|
|
v->status &= ~2u;
|
|
return SFUD_SUCCESS;
|
|
case 0xc7:
|
|
if ((v->status & 2) && !(v->status & 0x3c)) {
|
|
memset(v->memory, 0xff, v->capacity); v->status &= ~2u;
|
|
}
|
|
return SFUD_SUCCESS;
|
|
default: break;
|
|
}
|
|
v->bad_command++;
|
|
return rn ? SFUD_ERR_READ : SFUD_ERR_WRITE;
|
|
}
|
|
|
|
static sfud_err qread(const sfud_spi *spi, uint32_t a, sfud_qspi_read_cmd_format *f, uint8_t *r, size_t size) {
|
|
virtual_flash *v = spi->user_data;
|
|
unsigned bits = v->four_byte ? 32 : 24;
|
|
unsigned address_lines = 1, data_lines = 1, dummy = 0;
|
|
switch (f->instruction) {
|
|
case 0x13: bits = 32; break;
|
|
case 0x03: break;
|
|
case 0xbb: address_lines = 2; data_lines = 2; dummy = 8; break;
|
|
case 0xbc: address_lines = 2; data_lines = 2; dummy = 8; bits = 32; break;
|
|
case 0x3b: data_lines = 2; dummy = 8; break;
|
|
case 0x3c: data_lines = 2; dummy = 8; bits = 32; break;
|
|
case 0xeb: address_lines = 4; data_lines = 4; dummy = 6; break;
|
|
case 0xec: address_lines = 4; data_lines = 4; dummy = 6; bits = 32; break;
|
|
case 0x6b: data_lines = 4; dummy = 8; break;
|
|
case 0x6c: data_lines = 4; dummy = 8; bits = 32; break;
|
|
default:
|
|
v->bad_command++; return SFUD_ERR_READ;
|
|
}
|
|
if (f->instruction_lines != 1 || f->address_size != bits || f->address_lines != address_lines ||
|
|
f->data_lines != data_lines || f->dummy_cycles != dummy || f->alternate_bytes_lines != 0 ||
|
|
(uint64_t)a + size > v->capacity) {
|
|
v->bad_command++; return SFUD_ERR_READ;
|
|
}
|
|
if (size) memcpy(r, v->memory + a, size);
|
|
return SFUD_SUCCESS;
|
|
}
|
|
|
|
sfud_err sfud_spi_port_init(sfud_flash *f) {
|
|
virtual_flash *v = f->user_data;
|
|
f->spi.wr = bus; f->spi.qspi_read = qread; f->spi.user_data = v;
|
|
f->retry.times = 4;
|
|
if (v->configured) {
|
|
f->chip.name = "configured-NOR";
|
|
f->chip.mf_id = v->mf; f->chip.type_id = v->type; f->chip.capacity_id = v->density;
|
|
f->chip.capacity = v->capacity; f->chip.write_mode = SFUD_WM_PAGE_256B;
|
|
f->chip.erase_gran = 4096; f->chip.erase_gran_cmd = 0x20;
|
|
}
|
|
return SFUD_SUCCESS;
|
|
}
|
|
|
|
void sfud_log_debug(const char *file, const long line, const char *format, ...) {
|
|
(void)file; (void)line; (void)format;
|
|
}
|
|
void sfud_log_info(const char *format, ...) {(void)format;}
|
|
|
|
int vf_open(virtual_flash *v, sfud_flash *f, const char *profile, unsigned status, unsigned salt, int fault) {
|
|
uint32_t i;
|
|
memset(v, 0, sizeof(*v)); memset(f, 0, sizeof(*f));
|
|
v->mf = 0xef; v->type = 0x40; v->density = 0x17; v->capacity = 8u*1024*1024;
|
|
if (!strcmp(profile, "mx")) {
|
|
v->mf = 0xc2; v->type = 0x20; v->density = 0x16;
|
|
v->capacity = 4u*1024*1024; v->configured = 1;
|
|
} else if (!strcmp(profile, "large")) {
|
|
v->density = 0x19; v->capacity = 32u*1024*1024;
|
|
} else if (!strcmp(profile, "normal32") || !strcmp(profile, "normal64")) {
|
|
v->type = 0x70; v->density = 0x19;
|
|
v->capacity = !strcmp(profile, "normal64") ? 64u*1024*1024 : 32u*1024*1024;
|
|
v->configured = 1;
|
|
} else if (!strcmp(profile, "edge")) {
|
|
v->density = 0x18; v->capacity = 16u*1024*1024;
|
|
} else if (!strcmp(profile, "sst")) {
|
|
v->mf = 0xbf; v->type = 0x25; v->density = 0x8e; v->capacity = 1u*1024*1024;
|
|
} else if (strcmp(profile, "small")) return 0;
|
|
v->status = status & 0xfc; v->salt = salt; v->fault = fault;
|
|
v->memory = malloc(v->capacity);
|
|
if (!v->memory) return 0;
|
|
for (i = 0; i < v->capacity; i++) v->memory[i] = (uint8_t)((i*13u) ^ (i >> 9) ^ (i >> 24) ^ salt);
|
|
f->name = "virtual-nor"; f->spi.name = "software-spi"; f->user_data = v;
|
|
return 1;
|
|
}
|
|
void vf_close(virtual_flash *v) {free(v->memory);}
|